tags:

views:

517

answers:

1

I am trying to use a prepared statement that deletes from two (temporary) tables:

public function clearTempTables()  
{  
   static $delStmt = null;  
    if (null == $delStmt)  
    {  
        $delStmt = $this->pdo->prepare("DELETE FROM product_string_ids;   
                                        DELETE FROM product_dimension_value_ids;");  
    }  

    $delStmt->execute();
}

Calling this function succeeds, but when a statement is executed afterwards, I receive the following error:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  
Consider using PDOStatement::fetchAll().  
Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Setting PDO::MYSQL_ATTR_USE_BUFFERED_QUERY when creating my PDO object (as indicated in the docs and numerous web examples) seems to have no effect, and calling $delStmt->fetchAll() results in "SQLSTATE[HY000]: General error", which makes sense, since delete statements shouldn't return and results that need fetching anyway.

What am I doing wrong here? Is it even possible to prepare multiple SQL statements into a single statement like this? From a performance point of view it certainly makes sense, especially with a large number of queries that would do work on temporary tables and then only return a final result set.

+1  A: 

Afaik it's not possible to prepare multiple statements as one (combined) prepared statement.
But the DELETE syntax allows you to specify multiple tables to delete rows from.

$this->pdo->prepare("
  DELETE
    product_dimension_value_ids,product_string_ids
  FROM
    product_dimension_value_ids,product_string_ids
");

(now it's tested)

VolkerK
Thanks. It's really unfortunate that I can't bundle SQL statements together as it'll limit me for other stuff, but the DELETE syntax tip helps.Any idea how PDO handles stored procedures then? Aren't they essentially just collected statements?
FlintZA
Nope, looks like the delete from multiple table syntax isn't valid after all.
FlintZA
urgs, next try. Seems to be working, on MySQL 5.1.37
VolkerK
Yup, that did indeed work, thanks :)
FlintZA