php 当其他无缓冲查询处于活动状态时,PDO 无法执行查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2066714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO Cannot execute queries while other unbuffered queries are active
提问by Jim
I know this has to be a simple fix and I partially understand why I am getting this error but don't know how to fix it. I've looked over the docs but can't find a solution other than using buffered queries option. I have tried that as well but it doesn't work.
我知道这必须是一个简单的修复,我部分理解为什么我会收到此错误但不知道如何修复它。我查看了文档,但除了使用缓冲查询选项之外找不到其他解决方案。我也试过了,但它不起作用。
The error is: PDO Cannot execute queries while other unbuffered queries are active
错误是:PDO 无法执行查询,而其他未缓冲的查询处于活动状态
The error is coming from the line where I am building the $result array.
错误来自我构建 $result 数组的行。
foreach($phones as $phone)
{
$stmt = db::getInstance()->prepare("CALL phones(:phone)");
$stmt->bindParam(':phone', $phone, PDO::PARAM_INT, 10);
$stmt->execute();
$result[] = db::getInstance()->query("SELECT @phone;")->fetchAll(PDO::FETCH_ASSOC);
}
回答by Addsy
You need to free up your connection using the PDOStatement::closeCursor() method
您需要使用 PDOStatement::closeCursor() 方法释放连接
http://www.php.net/manual/en/pdostatement.closecursor.php
http://www.php.net/manual/en/pdostatement.closecursor.php
I believe
我相信
foreach($phones as $phone)
{
$stmt = db::getInstance()->prepare("CALL phones(:phone)");
$stmt->bindParam(':phone', $phone, PDO::PARAM_INT, 10);
$stmt->execute();
$stmt->closeCursor()
$result[] = db::getInstance()->query("SELECT @phone;")->fetchAll(PDO::FETCH_ASSOC);
}
should do it for you
应该为你做
回答by kmoney12
I ran into this problem due to an error in my PDO connection clause. I was trying to change the timezone upon connecting:
由于 PDO 连接子句中的错误,我遇到了这个问题。我试图在连接时更改时区:
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8; SET time_zone = '$timezone';"
I changed it to:
我把它改成:
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8, time_zone = '$timezone';"
And it worked fine.
它工作得很好。
回答by Merijn
I just ran into this problem myself and the problem turned out to be the use of stacked queries. The above solution did not solve the problem.
我自己刚刚遇到了这个问题,结果是使用了堆叠查询。上述解决方案没有解决问题。
We had this query running right before the one that triggered the error:
我们在触发错误之前运行了这个查询:
return $this->fquery('
SELECT @follow_id:=COALESCE(MAX(follow_id) + 1, 0) FROM sync_delete_value;
INSERT INTO sync_delete_value (...)
VALUES (%d, @follow_id, %d, "%s")',
$val1, $val2, $val3
);
Everything resumed as usual when I changed this into:
当我将其更改为:
$followId = $this->fquery('
SELECT @follow_id:=COALESCE(MAX(follow_id) + 1, 0) FROM sync_delete_value'
);
return $this->fquery('
INSERT INTO sync_delete_value (...)
VALUES (%d, %d, %d, "%s")',
$val1, $followId, $val2, $val3
);
);
It's sorta pseudo-code but you get the point.
这是一种伪代码,但你明白了。
回答by Patrick Fabrizius
If $stmt->closeCursor() does not work for you (it didn't for me), you can just unset the $stmt variable to free upp the cursor, like so:
如果 $stmt->closeCursor() 对您不起作用(对我不起作用),您可以取消设置 $stmt 变量以释放光标,如下所示:
foreach($phones as $phone)
{
$stmt = db::getInstance()->prepare("CALL phones(:phone)");
$stmt->bindParam(':phone', $phone, PDO::PARAM_INT, 10);
$stmt->execute();
unset($stmt);
$result[] = db::getInstance()->query("SELECT @phone;")->fetchAll(PDO::FETCH_ASSOC);
}

