PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 和准备好的语句中的多个删除语句
我正在尝试使用从两个(临时)表中删除的准备好的语句:
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();
}
调用此函数成功,但是随后执行语句时,我收到以下错误:
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.
在创建 PDO 对象时设置 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (如中所示)文档和大量网络示例)似乎没有效果,并且调用 $delStmt->fetchAll() 会导致“SQLSTATE[HY000]:一般错误”,这是有道理的,因为删除语句不应返回并且需要返回结果无论如何,都在获取。
我在这里做错了什么?是否有可能将多个 SQL 语句准备成这样的单个语句?从性能的角度来看,这当然是有意义的,特别是对于需要在临时表上执行操作然后仅返回最终结果集的大量查询。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,不可能将多个语句准备为一个(组合)准备好的语句。
但是 DELETE 语法 允许您指定要删除的多个表行来自。
(现在已经测试过了)
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.
(now it's tested)