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 Filtering Input for Queries Creating the Search Function

$total_items = get_catalog_count($section, $search);

My search won't work unless if I remove the second parameter, the $search variable. Then it works perfectly, but doesn't match up to teachers code and won't work with both $section and $search.

Any idea where I am going wrong?

You would have to at least post your code for get_catalog_count in order for someone to help.

KRIS NIKOLAISEN : I've posted my code below. There is a lot of code. I tried to post the most important stuff only.

1 Answer

Took me a while but I found the answer to my own question. In the if() statement, on the third line right below $sql, I didn't have an space before WHERE.

My code was "WHERE title LIKE ?" and it should have been " WHERE title LIKE ?".

I can't believe such a minor detail was driving me crazy. Was almost impossible to notice because there are no typos or spelling mistakes, just simply missed the empty space.

SQL was reading my code as "SELECT COUNT(media_id) FROM mediaWHERE title LIKE ?"

It's easier to pick up on this when the code is on one line, but our conditionals forced us to have it on a separate line.

CORRECT

if (!empty($search)) {
            $results = $handle->prepare(
            $sql
                . " WHERE title LIKE ?"
            );
            $results->bindValue(1, '%'.$search.'%', PDO::PARAM_STR);
        } 

PS. I am still confused why we need to pass $search to get_catalog_count. It works both with and without????

$total_items = get_catalog_count($section, $search);