afflik1923
asked on
PHP PDO mysql - cannot get the prepare statement to work - what's wrong?
Hi,
I'm experimenting with starting to use PDO which seems great. However I cannot get the prepare statment to work.
I have shown a snippet of code below which does not return any results when I use the variable substitution feature but when I replace the "?" characters with actual values it works fine.
I cannot see what I'm doing wrong but I'm sure it is something obvious. Can anyone see?
Note that this code comes from wihtin a class hence the use of $this in the example.
Hope this is clear and thanks in advance.
I'm experimenting with starting to use PDO which seems great. However I cannot get the prepare statment to work.
I have shown a snippet of code below which does not return any results when I use the variable substitution feature but when I replace the "?" characters with actual values it works fine.
I cannot see what I'm doing wrong but I'm sure it is something obvious. Can anyone see?
Note that this code comes from wihtin a class hence the use of $this in the example.
Hope this is clear and thanks in advance.
//$sql= 'SELECT * FROM mytable LIMIT 50 OFFSET 0'; # works if I use this
$sql= 'SELECT * FROM mytable LIMIT ? OFFSET ?';
try {
$st = $this->db->prepare($sql);#
$st->execute(array(50, 0));
} catch (PDOException $e) {
die($e->getMessage());
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
have you removed the parameters in the execute() statement ?
$st->execute();
$st->execute();
ASKER
Ahh, OK yes that has worked. Thank you. Now the ideall thing would be to get the bind with named variables working.
Therefore:
$sql= 'SELECT * FROM mytable LIMIT :rowlimit OFFSET :rowoffset ';
Then make the execute work. Is there another step I have to do when using the above statement.
Thanks again!
Therefore:
$sql= 'SELECT * FROM mytable LIMIT :rowlimit OFFSET :rowoffset ';
Then make the execute work. Is there another step I have to do when using the above statement.
Thanks again!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great stuff. Worked perfect. Thanks!
ASKER
http://uk2.php.net/manual/en/pdo.prepare.php
However even when I tried adding the two lines of code it still does not work.
Also I intilly tried binding the value using varibles
:limit :offset in place of the quesion marks and then passing the associative array bu still no luck.
Is there some external setting which can stop this working. Is it a problem that I'm using MySQL 4.1 (but am using PHP5)?
thanks