gpt4 book ai didi

mysql - Slim MySQL异常: SQLSTATE[42000]: Syntax error or access violation: 1064

转载 作者:行者123 更新时间:2023-11-30 01:36:06 25 4
gpt4 key购买 nike

我在 mysql 语法中遇到了奇怪的错误,这里的帖子都没有帮助我。我想获取表中的下 3 项,所以我做了这个函数:

$app->get('/items/:id/:nOf',  'getNextItem');
function getNextItem($id,$nOf) {
$sql = "SELECT * FROM `items` WHERE `id` > :id ORDER BY `id` LIMIT :nOf";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(":id", $id);
$stmt->bindParam(":nOf", $nOf);
$stmt->execute();
$item = $stmt->fetchObject();
$db = null;
echo json_encode($item);
} catch(PDOException $e) {
$result = array("status" => "error", "message" => 'Exception: ' . $e->getMessage(),"fnc"=>"getItems($id,$nOf)");
echo json_encode($result);
}
}

结束输出为:

{"status":"error",
"message":"Exception: 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 ''3''
at line 1","fnc":"getItems(1,3)"}

我没看出有什么问题。 Sql 命令在 phpmyadmin 中工作正常。 slim 论坛原帖 here .

最佳答案

尝试将 $nOf 绑定(bind)为整数:

$stmt->bindParam(":nOf", $nOf, PDO::PARAM_INT);

关于mysql - Slim MySQL异常: SQLSTATE[42000]: Syntax error or access violation: 1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16875503/

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