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 Using PHP with MySQL Limiting Records in SQL Queries Using LIKE

Leigh Maher
Leigh Maher
21,830 Points

No search results showing?

I'm having problems with the get_products_search function. When I try a search on any keyword it doesn't bring back any results. There's no errors. Just nothing coming back. Here's my code:

// This function will bring back the results of the search that someone does on the search form
function get_products_search($s) {

    require(ROOT_PATH . "inc/database.php");

    try {
        $results = $db->prepare('
            SELECT name, price, sku, paypal, img
            FROM products
            WHERE name LIKE ?
            ORDER BY sku
            ');
        $results->bindValue(1, "%" . $s . "%"); // bindValue allows us to concatonate strings to the variable $s
        $results->execute();
    } catch (Exception $e) {
        echo 'Data could not be retrieved';
        exit;
    }

    $matches = $results->fetchAll(PDO::FETCH_ASSOC);

    // and returns that array to where we called the function in the search/index.php page i.e. $products = get_products_search($search_term);
    return $matches;

}

Any ideas?

Everything looks right, have you tried var_dump()ing the result to see what pops out?

$shirts = get_products_search("red");
var_dump($shirts);

or some such

3 Answers

Paul Yorde
Paul Yorde
10,497 Points

Maybe this:

Your select statement is this:

SELECT name, price, sku, paypal, img

However in the video it is

SELECT name, price, img, sku, paypal

Also, if this makes any difference, you are using single quotes for the prepare statement, however the video uses double quotes:

// yours
$results = $db->prepare('

// video
$results = $db->prepare("
Leigh Maher
Leigh Maher
21,830 Points

Thanks Paul, but neither of these make any difference.

Leigh Maher
Leigh Maher
21,830 Points

Thanks Corey. I var_dumped the function output and it looked fine, so this meant it had to be something to do do with the display. Maybe I missed a video when he made some changes to the code in the partial products list view file, but I downloaded the finished project file and compared it to what I had. I had this:

// This function controls the output of the each product list item
function get_list_view_html($product) {

    $output = '';

    $output = $output . "<li>";
    $output = $output . '<a href="' . BASE_URL . 'shirts/' . $product["sku"] . '/">';
    $output = $output . '<img src="' . BASE_URL . $product["img"] . '" alt="' . $product["name"] . '">';
    $output = $output . "<p>View Details</p>";
    $output = $output . "</a>";
    $output = $output . "</li>";

    return $output;
}

I updated it to this:

?><li>
        <a href="<?php echo BASE_URL; ?>shirts/<?php echo $product["sku"]; ?>/">
            <img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
            <p>View Details</p>
        </a>
    </li>

And now it's displaying the products correctly.

Thanks for getting me to var_dump. This helped me solve it.

Successful debugging! I'm glad you got everything working.