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

运行环境: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();
	}

   //一些代码 
} 

 啦啦啦。。。

posted @ 2015-10-31 10:58  osfipin  阅读(1833)  评论(0编辑  收藏  举报
我是底部