PDO报错:Cannot execute queries while other unbuffered queries are active
用 PDOStatement->execute() 执行查询时出现错误:
Message: 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.
经过测试发现是因为在前一次 execute 里面执行了多条SQL语句(即通过分号分隔连续执行了多次的select/insert/update)。
http://blog.stormwild.net/2009/04/21/cannot-execute-queries-while-other-unbuffered-queries-are-active/
看上去应该是 Pdo_Mysql 库本身存在的问题?没有找到比较好的解决方案。唯一比较笨的方法是:在执行了 multi-query 语句之后立即 closeConnection ,断开本次数据库连接。再下次查询之前先重新建立连接。这样 unbuffered queries 就被重置了。
> reset query cache
也就是说,未结束之前的查询而再次查询,这样是会出错地,所以要释放掉之前的查询。
解决方法:
[php] view plaincopy
$configs = $this->config;
// 解决方法就是增加这句
$configs['params'][constant('PDO::MYSQL_ATTR_USE_BUFFERED_QUERY')] = true;
if ($this->pconnect) {
$configs ['params'] [constant ( 'PDO::ATTR_PERSISTENT' )] = true;
}
try {
$this->link = new PDO ( $configs ['dsn'], $configs ['username'], $configs ['password'], $configs ['params'] );
} catch ( PDOException $e ) {
throw new exception ( $e->getMessage () );
//exit('连接失败:'.$e->getMessage());
}