Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在PDO中使用%bindValue?_Php_Pdo_Bindvalue - Fatal编程技术网

Php 如何在PDO中使用%bindValue?

Php 如何在PDO中使用%bindValue?,php,pdo,bindvalue,Php,Pdo,Bindvalue,这就是我所拥有的 当我尝试bindValue,然后在准备好的语句%中使用它时,我有一个错误:queryString% $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

这就是我所拥有的

当我尝试bindValue,然后在准备好的语句%中使用它时,我有一个错误:queryString%

$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();
我怎样才能解决这个问题呢?

你应该这样做

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