Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Using Relational Tables Querying Multiple Tables with JOIN

Daniel Goldberg
Daniel Goldberg
12,443 Points

single_item_array function not running properly. Problem with LEFT OUTER JOIN

For some reason my single_item_array function is not running correctly. Whenever I attempt to load any details page, it is unable to retrieve the data it runs the exception instead. After playing around a bit I discovered that found that the problem lies in the LEFT OUTER JOIN line. When I delete this line (as well as publisher and isbn from the SELECT line) the code runs perfectly. As you can see below, my code is identical to the code shown in this video so I have no idea why it won't work. Please help!

My code:

function single_item_array($id){
  include("connection.php");
  try{
    $results = $db->query(
      "SELECT media_id, title, category, img, format, year, genre, publisher, isbn 
       FROM Media
       JOIN Genres ON Media.genre_id = Genres.genre_id
       LEFT OUTER JOIN Books ON Media.media_id = Books.media_id
       WHERE Media.media_id = $id"
       );
} catch (Exception $e){
    echo "Unable to retrieve results";
    exit; 
}
$output = $results->fetch();
return $output;
}

3 Answers

Nathaniel Kolenberg
Nathaniel Kolenberg
12,836 Points

Hi Daniel,

I had the exact same issue and I added Media.media_id (to make it non-ambiguous) and then it worked. This is in line with the difference that Simon Coates mentioned in his answer.

That should solve the issue. Let us know if it works/doesn't work for you.

Cheers,

N8

Simon Coates
Simon Coates
28,694 Points

the only difference i noticed running a text comparison of your version against the download version from the next video is some slight difference in variable naming and the media_id being Media.media_id for the initial field listing. So maybe need to address conflict with media id. The join i assume brings in a another field with the same name?

at the end, perhaps you have named the variable something else but instead of :

$output = $results->fetch(); return $output;

the video shows : $catalog = $results->fetch(); return $catalog;