我在一个界面里面进行了两次mysql数据库操作.
1.界面布局分成左右两块,进入界面的时候显示左侧内容,进行一次数据库操作。
2.点击左侧的submit,右侧内容显示,有进行一次数据库操作。
我有一个数据库类,在左边new一次,在右侧new一次。
进入页面就会出现Warning: mysql_close(): 7 is not a valid MySQL-Link resource in E:\www\bk_hmsystem\backstage\db.php on line 22 
如果我删除左侧的数据库操作就不会出现这个问题。
mysql_close放在数据库操作类的析构函数中,如下:
class db
{
    private $host;
    private $user;
    private $pw;
    private $con;
    private $dbname;
    
    function __construct($host,$user,$pw,$dbname)
    {
        $this->host=$host;
        $this->user=$user;
        $this->pw=$pw;
        $this->dbname=$dbname;
        $this->connect();
    }
    function connect()
    {
        $this->con=mysql_connect($this->host,$this->user,$this->pw); 
        if(!$this->con) die(mysql_error()); 
        mysql_select_db($this->dbname,$this->con) or die(mysql_error());
    }
    function __destruct()
    {
        mysql_close($this->con);
    }
    function query($name,$table,$cond)
    {
        if(($name=="")&&($cond==""))
        $sql="select * from $table";
        else 
        {
            if($name=="")
            $sql="select * from $table where $cond";
            else
            {
                if($cond=="")
                    $sql="select $name from $table";
                else
                $sql="select $name from $table where $cond";
            }
        }
        $result=mysql_query($sql,$this->con);
        if(!$result)
        {
            die (mysql_error());
        }
        else
        return $result;
    }
}
红色的地方有错.不明白为什么?

解决方案 »

  1.   

    你的页面是两个框架页构成的吗?加些代码分析一下
      function __destruct()
      {
        if(is_resource($this->con))
          mysql_close($this->con);
        else {
          echo $_SERVER['PHP_SELF'];
          var_dump($this->con);
        }
      }
      

  2.   

    谢谢。
    我发现__construst和__destruct分别调用了两次,而两次__construct出来的$con值是一样的,__destruct的时候又是对同一个连接destruct,所有第二次__destruct就会出错。但不知道怎么解决。
    有没有办法在__destruct中判$con这个资源表示符所对应的链接是否关闭呢?