2013-06-01 179 views
0

我在mysql語法中遇到了奇怪的錯誤,這裏的帖子無法幫助我。 我特林拿到桌子旁3項,所以我做了這個功能:Slim MySQL異常:SQLSTATE [42000]:語法錯誤或訪問衝突:1064

$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中工作正常。在苗條論壇here的原帖。

+0

[PDO製備語句引起LIMIT聲明錯誤(http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-約-pdo#15991623) –

+0

解決了謝謝:) – zajca

回答

0

嘗試綁定$nOf爲整數:

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

我用綁定爲整數和setAttribute(PDO :: ATTR_EMULATE_PREPARES,false);它的工作:) – zajca

相關問題