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_AT开发者_如何学CTR_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.
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)
精彩评论