0

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:

$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.

vidit
  • 6,293
  • 3
  • 32
  • 50
zajca
  • 2,288
  • 4
  • 30
  • 40
  • [PDO prepared statement causes an error in LIMIT statement](http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15991623) – Your Common Sense Jun 01 '13 at 17:50

1 Answers1

0

Try to bind $nOf as an integer:

$stmt->bindParam(":nOf", $nOf, PDO::PARAM_INT);
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • I did used bind as integer and setAttribute( PDO::ATTR_EMULATE_PREPARES, false ); and it's working :) – zajca Jun 02 '13 at 11:59