0

我在 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 中运行良好。苗条论坛上的原始帖子在这里

4

1 回答 1

0

尝试绑定$nOf为整数:

$stmt->bindParam(":nOf", $nOf, PDO::PARAM_INT);
于 2013-06-01T19:04:41.720 回答