Using PHP v. 5.2.14 and PDO-MySQL extension.
I am new to prepared statements. Need to create a search form (so user input) and a working query of the "Select all X where X like…" variety.
Code and Results:
$sql = 'SELECT COUNT(*) as num_books from t_books where title LIKE :search_term';
// Later access as num_books
$prep = $dbh->prepare($sql);
$num = $prep->execute(array(':search_term' => '%'.$search_term. '%'));
$total=$num->fetchColumn();
Var dump of $prep: object(PDOStatement)#2 (1) { ["queryString"]=> string(58) "SELECT COUNT(*) from t_books where title LIKE :search_term" }
Fatal error: Call to a member function fetchColumn() on a non-object
If $prep is an object, then $num should be an object. But it is boolean (true). What happened here?
But more importantly, how can I do a count?
I read Row count with PDO -. Suggestions there do not work for me because I need a Prepared Statement. Will have user input.
Thanks for your help.