Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.


if (!empty($search)) {
            $results = $handle->prepare(
                . " 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);