【发布时间】:2014-05-08 06:14:15
【问题描述】:
所以我正在构建一个搜索脚本并传递两个变量,但首先我想确保 SQL 查询是正确的,所以我现在对变量进行硬编码。所以我的变量是
$comma_separated = "'Alberta','Ontario'";
这将传递给查询,如下所示:
$sql = "SELECT * FROM persons WHERE 1=1";
if ($firstname)
$sql .= " AND firstname='" . mysqli_real_escape_string($mysqli,$firstname) . "'";
if ($surname)
$sql .= " AND surname='" . mysqli_real_escape_string($mysqli,$surname) . "'";
if ($province)
$sql .= " AND province='" . mysqli_real_escape_string($mysqli,$comma_separated) . "' WHERE province IN ($comma_separated)";
$sql .= " ORDER BY surname";
然后当查询运行时,我收到以下消息:
cannot run the query because: 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 'WHERE province IN ('Alberta','Ontario') ORDER BY surname LIMIT 0, 5' at line 1
但对我来说,查询看起来是正确的,我在这里缺少什么?
提前致谢。
【问题讨论】:
-
已编辑。请参见上文。
-
您在查询中使用了两次
WHERE
标签: php mysql if-statement