Link to home
Start Free TrialLog in
Avatar of afflik1923
afflik1923Flag for United Kingdom of Great Britain and Northern Ireland

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.
//$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()); 
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of psadac
psadac
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of afflik1923

ASKER

I'm very confused. I did not know I had to use the bind value as not mentioend on the PHP page
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
have you removed the parameters in the execute() statement ?

$st->execute();
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!

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great stuff. Worked perfect. Thanks!