Warning: mysql_close(): 7 is not a valid MySQL-Link resource in,该如何处理

WBOY
Release: 2016-06-13 13:34:13
Original
1215 people have browsed it

Warning: mysql_close(): 7 is not a valid MySQL-Link resource in
我在一个界面里面进行了两次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;
  }
}
红色的地方有错.不明白为什么?

------解决方案--------------------
显然调用了两次。第一次已经关闭连接。第二次就会出错。你干脆就省去那一步把。脚本结束后自然会断开连接的。
------解决方案--------------------
你的页面是两个框架页构成的吗?

加些代码分析一下
function __destruct()
{
if(is_resource($this->con))
mysql_close($this->con);
else {
echo $_SERVER['PHP_SELF'];
var_dump($this->con);
}
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!