Romantic Electron Romantic Electron - 1 year ago 42
MySQL Question

how to bind several columns to one variable in PDO like query?

I have a variable

and I want to search for records containing the
in multiple columns

$query="Select * from products where name LIKE ? OR color LIKE ?"
$stmt = $this->pdo->prepare($query);

but when I try to execute
, I get nothing displayed except for a '1'.The problem perhaps is binding of the
variable to the query.So I am curious whether there's a way to handle such a situation and what's the best working alternative.Thanks

Answer Source

$results is the success status of the PDOStatement execution; the returned rows are still in the $stmnt object. You need to iterate over the returned rows and print the individually.

while ($row = $stmnt->fetch(PDO::FETCH_ASSOC)) {

will work.