Personal Catalogue: PHP & MySQL — Chapter 6, Exercise 2 ==================================================== TASK Search with an empty search term but the type dropdown set to "cd." Explain, using the SQL your own PHP would build for this case, why every CD in the catalogue is returned even though the search box was left blank. SOLUTION With the search box empty and the type dropdown set to "cd" (assuming item_type_id 2, per the item_types seed data from Chapter 1), the URL is: search.php?q=&type=2&tag=0 $searchTerm ends up as '' (an empty string, after trim()). The PHP still builds: SELECT items.* FROM items WHERE (items.title LIKE :query OR items.creator LIKE :query) AND items.item_type_id = :type_id ORDER BY items.title params: ['query' => '%%', 'type_id' => 2] The key detail: $params['query'] is '%' . '' . '%', which evaluates to just '%' — the wildcard character on its own, with nothing between the two percent signs. In SQL's LIKE syntax, a bare '%' pattern means "match any sequence of characters, including zero characters" — so LIKE '%' matches every single row's own title (and every creator), regardless of what's actually in that column, including NULL-free empty strings. Since every CD's title genuinely matches LIKE '%', and the AND items.item_type_id = :type_id clause still correctly narrows the result down to only item_type_id = 2, the combined effect is: every row where LIKE '%' is true (all of them) AND item_type_id = 2 is true — which is exactly every CD in the catalogue. Confirm at the mysql> prompt: SELECT title FROM items WHERE title LIKE '%' AND item_type_id = 2; Expected output: every real CD title currently in the catalogue. WHY THIS WORKS AS AN ANSWER ---------------------------- It correctly identifies the specific mechanism (an empty search term collapses to the bare wildcard '%', which matches everything) rather than just observing that "it works," and confirms the type filter still narrows the otherwise-match-everything query down correctly.