gpt4 book ai didi

php - LIKE 的 SQL 错误

转载 作者:行者123 更新时间:2023-11-29 03:56:52 25 4
gpt4 key购买 nike

SELECT * FROM `orders` WHERE id LIKE %1%
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%1%' at line 1

PHP

$sql = "SELECT * FROM `orders` ";

switch ($_POST['criteria']) {

case 'id':
$sql .= "WHERE id LIKE %" . (int) $_POST['search_input'] . "%";
break;
case 'OCR':
$sql .= "WHERE OCR LIKE %" . $db->quote($_POST['search_input']) . "%";
break;
case 'name':
$arr = explode(' ', $_POST['search_input']);
$firstname = $arr[0];

if (isset($arr[1])) {
$lastname = $arr[1];
} else {
$lastname = null;
}

$sql .= "WHERE firstname LIKE %" . $db->quote($firstname) . "% AND lastname LIKE %" . $db->quote($lastname) . "%";
break;
}

echo $sql;

$stmt = $db->query($sql);

$rows = $stmt->fetchAll();

正在输出查询,对我来说它看起来很好,但由于某种原因我收到语法错误(我假设是),但我似乎无法发现任何问题?

最佳答案

LIKE运算符是一个字符串函数。所以你需要用单引号(')把它括起来。

SELECT * FROM `orders` WHERE id LIKE '%1%';

关于php - LIKE 的 SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com