开发者

How bindValue with % in PDO?

开发者 https://www.devze.com 2023-02-01 04:44 出处:网络
$query = $connect->prepare(\"SELECT users.firstname, users.lastname, users.id FROM users INNER JOIN users_friends ON users.id=users_friends.uID
$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 v开发者_C百科ariables does not match number of tokens'

How can i solve this?


You should do

"... LIKE :query ..." 

and then

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


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

精彩评论

暂无评论...
验证码 换一张
取 消