gpt4 book ai didi

php - MySQL 错误 1064 使用 LIMIT 子句

转载 作者:行者123 更新时间:2023-11-29 02:27:38 24 4
gpt4 key购买 nike

我在通过 PDO 准备好的语句运行查询时遇到了一个奇怪的问题,我无法发现这个问题。手动运行查询时,它工作得很好。这是代码(为简洁起见进行了简化):

// Query Parameters
$params = array( 1, 5 );

// Get Products
$query = "SELECT * FROM mydb.Product
WHERE ProductId >= ?
AND IsApproved = 1
AND IsPublic = 1
LIMIT ?";

// Get Database Instance
$dbh = App\App::getDatabase()->getInstance();

// Prepare Query
if( $stmt = $dbh->prepare($query) )
{
if( $stmt->execute($params) )
{
// Return Records
}
}

从查询中删除 LIMIT ? 部分会按预期返回所有结果。相反,当尝试使用 LIMIT 并且它传递 5 作为值时,我收到此错误:

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 ''5'' at line 5

准备后 PDOStatement 对象的转储显示:

object(PDOStatement)[59]
public 'queryString' => string 'SELECT * FROM mydb.Product
WHERE ProductId >= ?
AND IsApproved = 1
AND IsPublic = 1
LIMIT ?'

我试过在查询的末尾放一个分号,但会出现同样的错误。我是否患有脑胀气并遗漏了一些明显的东西?


TL;DR;为什么我的准备语句在使用 LIMIT 子句时失败并出现 1064 错误?

最佳答案

我认为这可能是重复的 PDO Mysql Syntax error 1064

解决方案是绑定(bind) limit 参数,强制它成为一个 int 而不是具有简单 (int) 转换的字符串。

关于php - MySQL 错误 1064 使用 LIMIT 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18722395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com