This question already has an answer here:
I have query like this:
SELECT imageurl
FROM entries
WHERE thumbdl IS NULL
LIMIT 10;
It works perfectly with PDO and MySQL Workbench (it returns 10 urls as I want).
However I tried to parametrize LIMIT
with PDO:
$cnt = 10;
$query = $this->link->prepare("
SELECT imageurl
FROM entries
WHERE imgdl is null
LIMIT ?
");
$query->bindValue(1, $cnt);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
That returns empty array.
I just tested a bunch of cases. I'm using PHP 5.3.15 on OS X, and querying MySQL 5.6.12.
Any combination works if you set:
All of the following work: you can use either an
int
or a string; you don't need to use PDO::PARAM_INT.You can also forget about bindValue() or bindParam(), and instead pass either an int or a string in an array argument to execute(). This works fine and does the same thing, but using an array is simpler and often more convenient to code.
If you enable emulated prepares, only one combination works: you must use an integer as the parameter and you must specify PDO::PARAM_INT:
Passing values to execute() doesn't work if you have emulated prepares enabled.
By default
bindValue
binds a string value but limit is an integer, so use PDO::PARAM_INT