PHP PDO "select like" query

Viewed 4038

I'm trying to bind two parameters to a prepared PDO select statement. This is what I have so far:

$query = "select * from books where ? LIKE ?";
$result = $db->prepare($query);
$result->bindValue(1, $searchTerm, PDO::PARAM_STR);
$result->bindValue(2, "%$searchValue%", PDO::PARAM_STR);
$result->execute();
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);

This executes successfully but I don't get any rows returned.

But when I use only 1 param

$query = "select * from books where title LIKE ?";
$result->bindValue(1, "%$searchValue%", PDO::PARAM_STR);

I get rows returned. I've checked 1000 times for my param values and names and they are fine.

I don't understand why this doesn't work with two params. Please advice.

1 Answers
Related