Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How bindValue with % in PDO?

Tags:

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

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?

like image 501
Karem Avatar asked Dec 28 '10 09:12

Karem


1 Answers

You should do

"... LIKE :query ..." 

and then

$query->bindValue(":query", $queryString); //where $queryString is '%someQuery%'
like image 171
Mironor Avatar answered Nov 06 '22 21:11

Mironor