Search code examples
phppdobindvalue

How bindValue with % in PDO?


$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();

This is what I have.

Im having error when I try to bindValue and then use it in the prepared statement ( %:queryString% )

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

How can i solve this?


Solution

  • You should do

    "... LIKE :query ..." 
    

    and then

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