php – 如何在PDO中使用%的bindValue?

$query = $connect->prepare("SELECT users.firstname, users.lastname, users.id 
FROM users INNER JOIN users_friends ON users.id=users_friends.uID
WHERE bID=:USER AND type =:type AND accepted = '1' AND (users.firstname LIKE '%:queryString%' OR users.lastname LIKE '%:queryString%') 
LIMIT 10");
$query->bindValue(":queryString", $queryString);
$query->bindValue(":type", $type);
$query->bindValue(":USER", $USER);
$query->execute();

这就是我所拥有的.

我尝试bindValue然后在预准备语句中使用它时出错(%:queryString%)

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens'

我怎么解决这个问题?

最佳答案
你应该做

"... LIKE :query ..." 

然后

$query->bindValue(":query", $queryString); //where $queryString is '%someQuery%'

转载注明原文:php – 如何在PDO中使用%的bindValue? - 代码日志