Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in you

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in you

  $wordsAry = explode(" ", $search);
$wordsCount = count($wordsAry);
$queryCondition = " WHERE ";
for($i=0;$i<$wordsCount;$i++) {
    $queryCondition .= "`location` LIKE '%$wordsAry[$i]%'";
    if($i!=$wordsCount-1) {
        $queryCondition .= " OR ";
    }
}
echo $queryCondition;

$sql1 = "SELECT * FROM `shelves_instruments` 
:szukaj
ORDER BY location ASC";
$licz_ilosc = $connect_db -> prepare($sql1);
$licz_ilosc -> bindValue(':szukaj', $queryCondition, PDO::PARAM_STR);
$licz_ilosc -> execute(); 
$ilosc_stron = $licz_ilosc->rowCount();

WHERE location LIKE '%walida%'

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' WHERE location LIKE '%walida%'' ORDER BY location ASC' at line 2 in

有什么问题吗?

准备好的语句并不是替换文本的好方法。您不能将整个 WHERE 子句作为单个参数发送。

您需要像这样创建一个 WHERE 子句:

$where = "WHERE `location` like :term1 OR `location` like :term2";

然后创建您的条款

     $param1 = '%'.$firstTerm.'%'  ;
     $param2 = '%'.$secondTerm.'%'  ;

然后将您的条款绑定到您准备好的声明中

$query = "SELECT * FROM `shelves_instruments` ".$where." ORDER BY location ASC"
$licz_ilosc = $connect_db -> prepare($query);
$licz_ilosc->execute([
    "term1"=>$param1,
    "term2"=>$param2
]);