Php 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1064

Php 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1064,php,mysql,pdo,prepared-statement,Php,Mysql,Pdo,Prepared Statement,我正在制作一个商店并使用一个输入来获得结果,现在我有了调用PHP脚本的AJAX,它调用它很好,但是我得到了一个错误: 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1064 注意:错误行是$query->EXECUTARRAY':input'=>$input行 下面是调用该函数的AJAX脚本+HTML <input type="text" name="search_item" onke

我正在制作一个商店并使用一个输入来获得结果,现在我有了调用PHP脚本的AJAX,它调用它很好,但是我得到了一个错误:

致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1064

注意:错误行是$query->EXECUTARRAY':input'=>$input行

下面是调用该函数的AJAX脚本+HTML

                     <input type="text" name="search_item" onkeyup="showItems(this.value)" id="search_item">
                     <script>
                        function showItems(str) {
                            if (str.length == 0) { 

                            } else {
                                var xmlhttp = new XMLHttpRequest();
                                xmlhttp.onreadystatechange = function() {
                                    if (this.readyState == 4 && this.status == 200) {
                                        document.getElementById("items").innerHTML = this.responseText;
                                    }
                                };
                                xmlhttp.open("GET", "searchScript.php?iName=" + str, true);
                                xmlhttp.send();
                            }
                        }
                    </script>

将通配符添加到参数:

$query = $con->prepare("SELECT ... WHERE product_name LIKE :input LIMIT 9 ");
$query->execute(array(':input' => '%' . $input. '%'));
这样,通配符就包含在值中,基本上使查询如下所示:

SELECT .... WHERE product_name LIKE '%name%'
您的查询结果类似于%'something',这是不正确的。将%添加到变量而不是查询中。你想要的是:

$input = "%$input%";

$query = $con->prepare("SELECT * FROM store AS s 
                        INNER JOIN product_pictures AS pp ON s.product_id = pp.id
                        INNER JOIN product_name AS pn ON s.product_id = pn.id
                        WHERE product_name LIKE :input LIMIT 9 ");
$query->execute(array(':input' => $input));

结果类似于%'something',这是不正确的。将%添加到变量而不是查询中。不能将通配符放在绑定的外部。在你输入之前把它放在周围。我仍然会收到一个错误。我正在编辑更新的PHP代码。哦,好的,我正在尝试很好-如果你认为这是最好的解决方案,请。要了解更多信息,请看哦,是的,我不得不等了几分钟,然后就忘了
$input = "%$input%";

$query = $con->prepare("SELECT * FROM store AS s 
                        INNER JOIN product_pictures AS pp ON s.product_id = pp.id
                        INNER JOIN product_name AS pn ON s.product_id = pn.id
                        WHERE product_name LIKE :input LIMIT 9 ");
$query->execute(array(':input' => $input));