如何在PDO中使用%的bindValue?

时间:2010-12-28 09:25:30

标签: 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'

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:13)

你应该做

"... LIKE :query ..." 

然后

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

答案 1 :(得分:0)

public function admin_search($conn,$search_key){

$stmt = $conn->prepare("
SELECT name,criteria,description,pic from brd_det WHERE name LIKE(:n1)
                         UNION

SELECT name,criteria,description,pic from cus_det WHERE name LIKE (:n2)
                             UNION

SELECT name,criteria,description,pic from doc_det WHERE name LIKE (:n3)
                             UNION

SELECT name,criteria,description,pic from par_det WHERE name LIKE (:n4)
                             UNION 

SELECT name,criteria,description,pic from pro_det WHERE name LIKE(:n5)
                             UNION 

SELECT name,criteria,description,pic from ser_det WHERE name LIKE (:n6)
                             "); 

     for ($i=1; $i < 7 ; $i++) { 

        $stmt->bindvalue('n'.$i,'%'.$search_key.'%'); 

     }
     //
    $result=$stmt->execute();