我对带参数的预准备语句有疑问,请参阅下文。
如果事实证明这是一个愚蠢的错误,我会事先道歉。
错误:
Failed to run query (Project List) - 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 ''0,8'' at line 1
代码:
$limit = ($pagenum - 1) * $page_rows . "," . $page_rows;
print_r($limit); // Prints 0,8 as expected
$query = "SELECT * FROM projects INNER JOIN users ON projects.companyid = users.companyid ORDER BY projects.projectid DESC LIMIT :limit";
$query_params = array (
':limit' => $limit
);
try {
$stmt = $db->prepare($query);
$stmt->execute($query_params);
}
catch (PDOException $ex) {
die("Failed to run query (Project List)" . " - " . $ex->getMessage());
}
到目前为止我尝试过的事情:
$limit
字符串LIMIT 0,8
在SQLyog中手动测试SQL查询 - 正常工作答案 0 :(得分:4)
您的$ limit参数作为一个参数进行转义,应该将其转义为两个参数。你的sql目前看起来像"限制' 0,8&#39 ;;"它看起来应该是什么样的"限制0,8&#34 ;;
要解决此问题,您应该将limit参数拆分为两个。编辑SQL的结尾看起来像:
LIMIT :offset, :limit
您的参数列表如下所示:
$query_params = array (
':offset' => ($pagenum - 1) * $page_rows,
':limit' => $page_rows
);
史密斯先生提到,你还必须加上这句话:
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
确保将限制参数正确地处理为整数而不是字符串。