This question already has answers here:
LIMIT keyword on MySQL with prepared statement [duplicate]
                                
                                    (2个答案)
                                
                        
                                5年前关闭。
            
                    
我在查询时遇到问题:

$sth = $Db->dbh->prepare(
   "SELECT *
   FROM loader
   WHERE download = 0
    AND lastconnected BETWEEN DATE_SUB(NOW(),INTERVAL 15 MINUTE) AND NOW()
   ORDER BY lastconnected DESC
   LIMIT :amount");


LIMIT由于某些原因不起作用,如果我将:amount更改为硬编码数字,它将起作用,但是当我将其用作:amount时,就会出现此错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''1'' at line 5


这是我用来执行准备好的查询的内容:

$sth->execute(array(':amount' => $amount));


一直试图弄清楚几个小时。希望有人能看到我没有的东西。

最佳答案

如果在PDO中使用变量LIMIT,则必须将每个参数与PDOStatement::bindParam()绑定,并明确指定参数的值具有整数类型(PDO::PARAM_INT)。带有输入参数值数组的PDOStatement::execute()会将所有值都视为字符串(PDO::PARAM_STR),而不是实际的PHP类型,就像不带类型调用PDOStatement::bindParam()一样,但是MySQL的LIMIT关键字不接受字符串参数。 the manual page for the method PDOStatement::execute()中对此进行了说明,并且有一个打开的feature request to change the behavior

关于mysql - MySQL LIMIT子句中的语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26761751/

10-11 05:25
查看更多