PDO Mysql语法错误1064 [英] PDO Mysql Syntax error 1064
问题描述
我运行以下代码:
$conn = new PDO(....);
.... pdo attributes ...
$limitvalue = 0;
$limit = 10;
$sql = $conn->prepare("SELECT * FROM table1 LIMIT ?, ?");
$sql->bindParam(1, $limitvalue, PDO::PARAM_INT);
$sql->bindParam(2, $limit, PDO::PARAM_INT);
$sql->execute();
我得到:
带有消息"SQLSTATE [42000]"的未捕获异常"PDOException": 语法错误或访问冲突:1064您的SQL中有错误 句法;检查与您的MySQL服务器版本相对应的手册 在第1行的'NULL,10'附近使用正确的语法
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 'NULL, 10' at line 1'
仅在此特定查询中发生.一切都很好.
It happens only with this particular query. Everything else is OK.
顺便说一句:我知道我为"in-code"值准备了语句可能看起来很愚蠢.但这只是一个例子.实际上,值取决于页码,但在这里并不重要-此查询也给出了相同的错误.
Btw: I know it may look stupid that i do prepared statements for "in-code" values. But it is just an example. In fact the values are depending on the page number but it doesn't matter here - this query is giving the same error too.
如果有人感兴趣,则PHP版本为: 5.3.4RC2 ,而MySQL版本为: mysqlnd 5.0.7-dev-091210-$ Revision:304625 $ >
If anybody is interested, the PHP version is: 5.3.4RC2 and MySQL's is: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
推荐答案
这似乎是 php bug :PDO忽略PARAM_INT常量,并使用$limit
和$limitvalue
变量作为字符串.绑定时在查询中引用哪个.
This seems to be a php bug : PDO ignores the PARAM_INT constant and use the $limit
and $limitvalue
variables as string. Which are quoted in the query when bound.
尝试使用:
$sql->bindParam(1, (int)$limitvalue, PDO::PARAM_INT);
$sql->bindParam(2, (int)$limit, PDO::PARAM_INT);
强制变量类型为int.
To force the variables type to int.
这篇关于PDO Mysql语法错误1064的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!