一次查询调用中的 PDO 多个语句以“无缓冲查询”错误结束
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果查询包含以分号分隔的多个语句并且在无缓冲模式下执行,则会出现错误“当其他无缓冲查询处于活动状态时无法执行查询”。
解决方案是:
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: