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();
查询正在输出,对我来说看起来不错,但由于某种原因,我遇到了语法错误(我认为是),但是我似乎没有发现任何问题?