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 Understanding SQL Injections

Prince Henry
Prince Henry
4,312 Points

I have this error "Unable to retrieve results" on my browser.

I have followed the steps to this point but since from the last video, i keep having this error message on by browser "Unable to retrieve results". Can someone help me out?

Here is my code....

<?php

function full_catalog_array() { include("connection.php");

try { $results = $db->query("SELECT media_id, title, category, img FROM Media"); } catch (Exception $e) { echo "Unable to retrieve results"; exit; }

$catalog = $results->fetchAll(); return $catalog; }

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; }

$catalog = $results->fetch(); return $catalog; } var_dump(single_item_array(1));

function get_item_html($id,$item) { $output = "<li><a href='details.php?id=" . $id . "'><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; return $output; }

function array_category($catalog,$category) { $output = array();

foreach ($catalog as $id => $item) {
    if ($category == null OR strtolower($category) == strtolower($item["category"])) {
        $sort = $item["title"];
        $sort = ltrim($sort,"The ");
        $sort = ltrim($sort,"A ");
        $sort = ltrim($sort,"An ");
        $output[$id] = $sort;            
    }
}

asort($output);
return array_keys($output);

}

paste your code here

Hey Prince Henry, In your function single_item_array

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" 
                                       ); 
}

Here in your query you are joining two tables with media table i.e. Genres or Books & media_id is common in Media & Books .So while selecting the feild media_id you need to denote the table name from where you are capturing media_id.

try { 
$results = $db->query( "SELECT Media.media_id/Genres.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" 
                                       ); 
}

1 Answer

Try replacing this code: 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; }

With this block of code: try { $results = $db->query( "SELECT Media.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 retrieved results"; exit; }

I had the same problem and the solution in my case was to replace SELECT media_id with SELECT Media.media_id.