我在mysql语法中遇到奇怪的错误,这里的帖子没有帮助我。 我想把下三个项目放在桌子上,所以我做了这个功能:
$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)"}
我没有看到任何错误。在phpmyadmin中,Sql命令工作正常。苗条论坛here上的原帖。
答案 0 :(得分:0)
尝试将$nOf
绑定为整数:
$stmt->bindParam(":nOf", $nOf, PDO::PARAM_INT);