PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 和准备好的语句中的多个删除语句

发布于 2024-08-20 00:26:50 字数 1006 浏览 4 评论 0原文

我正在尝试使用从两个(临时)表中删除的准备好的语句:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-08-27 00:26:50

据我所知,不可能将多个语句准备为一个(组合)准备好的语句。
但是 DELETE 语法 允许您指定要删除的多个表行来自。

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

(现在已经测试过了)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文