PDO problem with LIMIT


I’m new to PDO and prepared statements. I recently tried to do something like this:

$pdo = $db->prepare('SELECT * FROM table LIMIT ?');

This gives me an error saying there is a problem with my SQL syntax near the use of “5”. Can’t I use prepared statements with LIMIT? (The same problem occurs with ORDER BY)

I’m using PHP 5.2.3

You cannot do prepared statements with LIMIT only WHERE.

Okay, too bad but thanks for the answer.

I bow to logic_earths’ knowledge on this as I have never tried it, still, in the example you show us - I can’t see where you are binding the value in your prepare() statement?

$pdo = $db->prepare('SELECT * FROM table LIMIT ?');
$pdo->bindValue(1, 5);

He passes them into $pdo->execute()

Sure it can. Do it all the time. Whats probably happening is that LIMIT 5 is actually being generated as LIMIT ‘5’. Use bind parameter and specify a data type.

$pdo->bindValue(1, 5, PDO::PARAM_INT);