mysql_free_result(): 2 is not a valid MySQL result resource
为啥???
下面是源码....救救我
public function query($str){
      if ($this -> Num > 10){
       mysql_free_result($this -> result) or die('释放不成功>>');
        echo '有数据缓存::'.$this -> Num;
      }else{
      $this -> result = mysql_query($str);
        echo $this -> Num = mysql_num_rows($this -> result);
      }
    }
有两个资源吗?但是我close也消除不了,用的是单例

解决方案 »

  1.   

    好像有个误区,应该是查询资源问题,不是连接标识,那是不是应该释放Num和Result变量资源?可惜,现在在外面没办法做测试了…万望解答,谢谢!
      

  2.   

    解决,我模仿了ThinkPHP的释放模式,设置一个free方法,大概放在同一个方法有冲突把....呵呵    public function free(){
          echo mysql_free_result($this -> result) ? '释放成功>>>' : die('数据库资源未被释放');
          $this -> result = null;
        }
        
        public function query($str){
          if ($this -> Num > 10){//设置查询资源条目上限,超过上限则释放资源
            $this -> free();
            echo '有数据缓存::'.$this -> Num.'<br/>';
          }
          
          $this -> result = mysql_query($str);
            echo $this -> Num = mysql_num_rows($this -> result);
            echo '<br/>';
          
        }
      

  3.   

    瞎写的一个db,呵呵....做个单例的实验,不过还是没有达到目标......
    <?php
      final class dbdrv extends db{
        protected function dbdrv(){
          //construct of the db class
            //why ? not new the dbdrv class
          $this -> conn('localhost','root',123);
          $this -> dbs('test');
          $this -> set('GBK');
        }
        
        private function conn($H,$U,$P){
          //Connect the DataBase
          mysql_connect($H,$U,$P);
        }
        
        public function dbs($db){
          //select the DataBase Name
          mysql_select_db($db);
        }
        
        public function set($set){
          mysql_query("SET NAMES $set");
        }
        
        public function free(){
          echo mysql_free_result($this -> result) ? '释放成功>>>' : die('数据库资源未被释放');
          $this -> result = null;
        }
        
        public function query($str){
          if ($this -> Num > 10){
            $this -> free();
            echo '有数据缓存::'.$this -> Num.'<br/>';
          }
          
          $this -> result = mysql_query($str);
            echo $this -> Num = mysql_num_rows($this -> result);
            echo '<br/>';
          
        }
      }
      
      class db{
        public $Num;
        public $result;
        private static $instance = null;
        //the instance the dbdrv class for singleton
        private function dbdrv(){//null
        }
        
        public function inc(){
         if ( null == self::$instance ) self::$instance = new dbdrv();
         return self::$instance; 
        }
        
        public function num(){
          echo '缓存的数据::'.self::$instance -> Num;
        }
      }
      
      
      //test
      db::inc() -> query('SELECT * FROM db');
      db::inc() -> query('select * from hh');
      db::inc() -> query('select * from hh');
      echo '<hr>';
      // $d = new db();
      //$d -> num();
    ?>