码迷,mamicode.com
首页 > 数据库 > 详细

pdo mysql错误:Cannot execute queries while other unbuffered queries are active

时间:2015-10-31 11:19:29      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

运行环境:PHP 5.5.30-x64,MYSQL  5.6.27

错误代码: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)。也就是说,未结束之前的查询而再次查询,这样是会出错地,所以要释放掉之前的查询。

手册上的代码

<?php
 if ( $db -> getAttribute ( PDO :: ATTR_DRIVER_NAME ) ==  ‘mysql‘ ) {
     $stmt  =  $db -> prepare ( ‘select * from foo‘ ,
        array( PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY  =>  true ));
} else {
    die( "my application only works with mysql; I should use \$stmt->fetchAll() instead" );
}
//http://www.cnblogs.com/osfipin/
 ?> 

If this attribute is set to TRUE on a PDOStatement, the MySQL driver will use the buffered versions of the MySQL API. If you‘re writing portable code, you should use PDOStatement::fetchAll() instead. 

测试了很多次还是有问题。。。。。

代码运行多个query,OPTIMIZE TABLE,DELETE操作。发现是OPTIMIZE TABLE导致的此错误。注释掉后代码运行无误。

使用OPTIMIZE TABLE:

<?php
class database extends PDO{
	//表前缀
	public $prefix = ‘‘;

        //一些代码

	//优化表
	function optimize($table){
		$stmt  =  $this -> prepare (‘OPTIMIZE TABLE `‘.$this->prefix.$table.‘`‘,array( PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY  =>  true ));
		return $stmt -> execute();
	}

   //一些代码 
} 

 啦啦啦。。。

pdo mysql错误:Cannot execute queries while other unbuffered queries are active

标签:

原文地址:http://www.cnblogs.com/osfipin/p/4925133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!