Slim MySQL异常:SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2021-09-18 14:35:28

I'm getting strange error in mysql syntax, non of the posts here helps me. I'm tring to get next 3 items in table, so I made this function:

我在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);
  }
}

End the output is:

结束输出是:

{"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)"}

I don't see anything wrong. Sql command is working fine in phpmyadmin. Original post on slim forum here.

我没有看到任何错误。在phpmyadmin中,Sql命令工作正常。这里有苗条论坛的原帖。

1 个解决方案

#1


0  

Try to bind $nOf as an integer:

尝试将$ nOf绑定为整数:

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

#1


0  

Try to bind $nOf as an integer:

尝试将$ nOf绑定为整数:

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