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

本文探讨了在使用PHP5.5.30-x64和MySQL 5.6.27时,遇到的Cannot execute queries while other unbuffered queries are active错误的原因和解决方案。重点分析了在执行多次SQL查询时,特别是执行了OPTIMIZETABLE操作后引发的问题,并提供了设置PDO属性以避免此类冲突的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

   //一些代码 
} 

 啦啦啦。。。

转载于:https://www.cnblogs.com/osfipin/p/4925133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值