一次查询调用中的 PDO 多个语句以“无缓冲查询”错误结束

发布于 2024-10-17 10:10:51 字数 1713 浏览 2 评论 0原文

我正在尝试在 PHP/PDO for MySQL 中实现一些嵌套集合操作。进行插入操作后(如下所述:http://mikehillyer.com/ articles/managing-hierarchical-data-in-mysql/) 由于错误 HY000/2014,我无法执行另一个查询:

无法在其他情况下执行查询 无缓冲查询处于活动状态。 考虑使用 PDOStatement::fetchAll()。 或者,如果您的代码只是 曾经要对抗mysql,你 可以通过设置启用查询缓冲 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 属性。

产生错误的代码(错误在代码底部的最后一个“SELECT * FROM tree”处抛出):

// insert a node into a nested set as child
$queryParts = array(
    'LOCK TABLE tree WRITE',
    'SELECT @myLeft:= lft FROM tree WHERE id = :parentuid',
    'UPDATE tree SET rgt = rgt + 2 WHERE rgt > @myLeft',
    'UPDATE tree SET lft = lft + 2 WHERE lft > @myLeft',
    'UPDATE tree SET lft = @myLeft + 1, rgt = @myLeft + 2 WHERE id = :childuid',
    'UNLOCK TABLES',
);

$parentuid = $parent->getUid();
$childuid = $node->getUid();

$stmt = self::$connection->prepare(join(";\n", $queryParts) . ';') or die (print_r(self::$connection->errorCode(),1));
$stmt->bindParam(':parentuid', $parentuid);
$stmt->bindParam(':childuid', $childuid);

$res = $stmt->execute() or die (print_r($stmt->errorCode(),1));

// that does not help :(
if ($res) {
    do {
        $stmt->fetchAll(); // empty array
        $stmt->closeCursor();
    } while($stmt->nextRowset());
}

// is not executed
$rset = self::$connection->query('SELECT * FROM tree')
     or die (print_r(self::$connection->errorInfo(),1)); // that kills the code
while($row = $rset->fetchObject()) {
    var_dump($row);
}

在我看来,没有打开的查询。 如何解决此错误? 目前我陷入困境,因此我们将不胜感激:)

I am trying to implement some nested set operations in PHP/PDO for MySQL. After making an insert operation (like stated here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) I am unable to execute another query because of error HY000/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.

The code wich produces the error (the error is thrown at the last "SELECT * FROM tree" at the bottom of the code):

// insert a node into a nested set as child
$queryParts = array(
    'LOCK TABLE tree WRITE',
    'SELECT @myLeft:= lft FROM tree WHERE id = :parentuid',
    'UPDATE tree SET rgt = rgt + 2 WHERE rgt > @myLeft',
    'UPDATE tree SET lft = lft + 2 WHERE lft > @myLeft',
    'UPDATE tree SET lft = @myLeft + 1, rgt = @myLeft + 2 WHERE id = :childuid',
    'UNLOCK TABLES',
);

$parentuid = $parent->getUid();
$childuid = $node->getUid();

$stmt = self::$connection->prepare(join(";\n", $queryParts) . ';') or die (print_r(self::$connection->errorCode(),1));
$stmt->bindParam(':parentuid', $parentuid);
$stmt->bindParam(':childuid', $childuid);

$res = $stmt->execute() or die (print_r($stmt->errorCode(),1));

// that does not help :(
if ($res) {
    do {
        $stmt->fetchAll(); // empty array
        $stmt->closeCursor();
    } while($stmt->nextRowset());
}

// is not executed
$rset = self::$connection->query('SELECT * FROM tree')
     or die (print_r(self::$connection->errorInfo(),1)); // that kills the code
while($row = $rset->fetchObject()) {
    var_dump($row);
}

In my opinion there is no open query. How to solve this error? Currently I am stucked, so any help would be highly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自在安然 2024-10-24 10:10:51

如果查询包含以分号分隔的多个语句并且在无缓冲模式下执行,则会出现错误“当其他无缓冲查询处于活动状态时无法执行查询”。

解决方案是:

  • 单独执行每个查询部分,每个部分应该是一个不带分号“;”的单个查询。
  • 或切换到缓冲模式。

The error "Cannot execute queries while other unbuffered queries are active" occurs if the query contains multiple statements separated by a semicolon and is executed in unbuffered mode.

Solution would be:

  • execute each query part separately, each part should be a single query without semicolon ";"
  • or switch to buffered mode.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文