mysql prepare/execute woes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a problem with some SQL code that can be run at the mySQL
command prompt, and in PHP, I can prepare it with no problem, but I
get a syntax error when I try and exexute it.

$sql = <<< EOF
    SELECT * from ad where id = ? and
    ((endDate is null) or (endDate is not null and endDate >=

$query = $dbh->prepare($sql);
if (DB::iserror($query)) {
    echo "Query error, " . $query->getMessage() . " please contact
# works, no compaints, but a few steps later

foreach ($controls as $c) {
    $e = $dbh->execute($query, $c);
    if (DB::iserror($e)) {
        echo "Execute error" . $e->getMessage() . " please contact
    $e = $e->fetchRow(DB_FETCHMODE_ASSOC);

I get this in my browser:

Execute error DB Error: syntax error please contact admin.

Now, if the sql was broken, I could understand the error, but I would
kind of have expected the prepare to fail.

This works OK if I leave off everything after id = $.

Any ideas?

Thanks in advance,

Re: mysql prepare/execute woes

Quoted text here. Click to load it

You are using heredoc syntax, the "; is not needed.

Re: mysql prepare/execute woes

CJ Llewellyn wrote:

Quoted text here. Click to load it

If you want to see what query it actually tried to run then do the

        print $dbh->last_query;

Really useful for seeing what you've done wrong. I'm sure if you'd printed
it out in this way you probably would have caught that error straight away.

Chris Hope - The Electric Toolbox - /

Re: mysql prepare/execute woes

On 3 Aug 2004 22:42:28 -0700, (Randy Rodent) wrote:

Quoted text here. Click to load it

 Others have addressed the problem with the SQL, however the reason why it
doesn't fail on prepare is that MySQL doesn't support prepared statements until
4.1, and only then through the mysqli extension.

 For databases that don't support prepared statements, DB::prepare only sets
things up on the client side, and it's not until execute that it actually hits
the server.

 < Space: disk usage analysis tool
  (v1.4.0 new 1st Aug 2004)

Site Timeline