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();
    	}
    
       //一些代码 
    } 
    

     啦啦啦。。。

  • 相关阅读:
    讨论: TDD in HTML & JavaScript 之可行性和最佳实践
    Enterprise Caching Summary
    NIntegrate SOA Practice – EAppointments Sample Application
    This is jqMVC# Definition & Summary
    LRU Caching Practice
    NIntegrate v0.9.0.1 released
    CrossDomain AJAX Enabled WCF Service
    突然发现想在博客园发起一个有价值的讨论其实很难
    Discuss some new features in WCF 4.0
    This is jqMVC# – CNBLOGS Google Tracer Sample
  • 原文地址:https://www.cnblogs.com/osfipin/p/4925133.html
Copyright © 2011-2022 走看看