当其他未缓冲的查询处于活动状态时,无法执行查询.考虑使用PDOStatement :: fetchAll().或者,如果您的代码只是针对mysql运行,则可以通过设置PDO :: MysqL_ATTR_USE_BUFFERED_QUERY属性来启用查询缓冲.
在PDO上.正如在提到的许多线程中一样,错误可能至少是以下问题之一:
>查询游标未使用此处提到的closeCursor()关闭; Causes of MySQL error 2014 Cannot execute queries while other unbuffered queries are active
>有两个以上的查询,其中一个声明如下所述:PDO Cannot execute queries while other unbuffered queries are active
>这里提到的MysqL-driver中的一个错误:What is causing PDO error Cannot execute queries while other unbuffered queries are active?
在我的情况下,所有上述都没有帮助,我花了一些时间才解决了问题.这是我的代码(伪代码):
$stmt->startTransaction();
$stmt = db::getInstance()->prepare("CALL phones(:phone)");
$stmt->prepare('SELECT * FROM database');
$stmt->execute();
$aData = $stmt->fetchAll();
$stmt->closeCursor();
$stmt->query("USE soMetable;");
我把它改成后:
$stmt->startTransaction();
$stmt = db::getInstance()->prepare("CALL phones(:phone)");
$stmt->prepare('SELECT * FROM database');
$stmt->execute();
$aData = $stmt->fetchAll();
$stmt->closeCursor();
$stmt->exec("USE soMetable;");
它适用于我的. query和exec有什么区别?
PDO::exec() - "Execute an sql statement and return the number of affected rows"
PDO::query() - "Executes an sql statement, returning a result set as a PDOStatement object"
为什么在这种情况下PDO :: query()不起作用?调用时,光标IS关闭.
解决方法:
虽然你可能已经在这里遇到了MysqL驱动程序错误,但我们无法确定这一点,因为你没有给我们这些信息(你使用的是哪个版本的PHP?它是否使用MysqLnd =>用PHP -i检查| grep MysqLnd?你的代码的其余部分是什么样的?).
您的问题还有许多其他可能的解释.我怀疑问题实际上是你没有关闭所有游标,和/或获取所有结果,因为$stmt被大量重用:
直接引自PDO :: query manual page:
If you do not fetch all of the data in a result set before issuing your next call to
PDO::query()
, your call may fail. CallPDOStatement::closeCursor()
to release the database resources associated with thePDOStatement
object before issuing your next call toPDO::query()
.
你在$stmt上调用closeCursor,这是真的,但是你没有关闭你创建的所有游标:
//<-- what is $stmt here?
$stmt->startTransaction();
//no matter, you've reassigned it a PDOStatement instance
$stmt = db::getInstance()->prepare("CALL phones(:phone)");
//Huh? You're preparing yet another query on an instance of PDOStatement?
$stmt->prepare('SELECT * FROM database');
//you're executing this one, though
$stmt->execute();
//and fetching all data
$aData = $stmt->fetchAll();
//and closing this last statement
$stmt->closeCursor();
但是你分配给$stmt的第一个语句(存储过程调用)怎么样?该光标未在任何地方关闭
现在为PDO :: query和PDO :: exec之间的主要区别.再次引用手册:
PDO::exec()
does not return results from a SELECT statement.
鉴于:
PDO::query()
executes an sql statement in a single function call, returning the result set (if any) returned by the statement as aPDOStatement
object.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。