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();
	}

   //一些代码
}

 啦啦啦。。。

时间: 2024-08-08 05:38:46

pdo mysql错误:Cannot execute queries while other unbuffered queries are active的相关文章

centos7.2 pdo mysql扩展

前言 选择是一个崭新的开端,选择高耸入云的峭崖便需有"路漫漫其修远兮,吾将上下而求索"的信念;选择波涌浪滚的大海便需有"直挂云帆济沧海"的壮志豪情;选择寒风劲厉的荒漠便需有"醉卧沙场君莫笑,古来征战几人回"的博大胸怀----- 一 .安装pdo-mysql错误1 首先下载解压包 把安装包指定路径 1. tar zvxf /root/media/var/PDP_MYSQL 2. cd /root/media/var/PDO_MYSQL 3. 记住先

mysql错误:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement解决方法

Mysql关系型数据库管理系统 MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司.MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库. 本文为大家讲解的是mysql错误:The MySQL server is running with the --skip-grant-tables option so it cannot

php PDO mysql写法

php PDO写法连接mysql: 写法一: $db="mysql:host=localhost;dbname=sql" : //连接数据,地址localhost:数据库名称sql: $username="root"; //数据库登录账号: $password="root"; //数据库登录密码: try{ $pdo=new PDO($db,$username,$password);   //连接数据库赋值$pdo; }catch(PDOExce

PHP扩展PDO MySQL之PDOStatement::bindParam vs bindValue

PHP扩展PDO MySQL之PDOStatement::bindParam vs bindValue技术 maybe yes 发表于2015-11-16 13:27 原文链接 : http://blog.lmlphp.com/archives/155/The_difference_of_PDOStatement_bindParam_and_bindValue_of_PHP_extension_PDO_MySQL  来自 : LMLPHP后院 前些日子将 LBlog 在线体验站点 http://

mysql错误:Error Code: 1175. You are using safe update mode and you tried to update a table……

今天遇到一个mysql错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect. 网上查了一下,原来是SET SQL_SAFE_UPDATES

php pdo mysql数据库操作类

原文:php pdo mysql数据库操作类 findAll(array( 'field'=>'ID,albumName,albumImage, mainActor,directors,tags,info,area, keywords,wflag,year,mod_version,totalDuration', 'where'=>$where, 'order'=>'flag desc,hit_count desc', 'limit'=>"{$limit['offset']

thinkphp获取mysql错误

最近用thinkphp时发现无法捕获mysql的错误信息,例如向表中插入一个主键 重复的数据时,程序便卡在那条语句上,就像下面这样 :( SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' 错误位置 FILE: D:\*****\ThinkPHP\Library\Think\Db\Driver.class.php LINE: 226 TRACE #0 D:\****

PHP PDO的错误处理模式

PDO默认的错误处理方式是返回一个数组格式的错误代码.如果想要判断SQL有没有出错时,就需要写一组代码来检测这个返回的数组.觉得这样有些麻烦.还好PDO还提供了另外两种处理方式,只要根据需要设置一下就OK了. 方式一:PDO::ERRMODE_SILENT 这是默认使用的模式.PDO会在statement和database对象上设定简单的错误代号,可以使用PDO->errorCode() 和 PDO->errorInfo() 方法检查错误: 方式二:PDO::ERRMODE_WARNING 使

Mysql错误编码和解释表

Mysql错误编码和解释表1.错误消息的组成 一共由三部分组成 mysql数字错误代码,这个数字的mysql的特定值,不可以移植到其它数据库系统. 一个五位长度的sqlstate值,这些值取自ANSI SQL和ODBC,并且更加标准化.并不是所有的MySQL错误号都具有相应的SQLSTATE值.在这些情况下,使用'HY000' (一般错误). 错误的消息描述 2.一些额外的说明 如果需要错误检查,请使用错误代码,而不是错误消息. 如果数据库管理员更改语言设置,则会影响错误消息的语言. 错误代码在