zoukankan      html  css  js  c++  java
  • 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();
    	}
    
       //一些代码 
    } 
    

     啦啦啦。。。

  • 相关阅读:
    Javaweb 第4 天xml 课程
    Javaweb 第2天 JavaScript课程
    Javaweb 第1天 HTML和CSS课程
    第27天反射(解剖)技术
    Ip 讲解
    第26 天网络编程
    第25天多线程、网络编程
    【剑指offer】连续子数组的最大和,C++实现
    [剑指offer]数组中最小的K个数,C++实现
    【剑指offer】数组中出现次数超过数组长度一半的数字,C++实现
  • 原文地址:https://www.cnblogs.com/osfipin/p/4925133.html
Copyright © 2011-2022 走看看