connect - php mysql 连接问题,不可以同时生成两个连接么。

WBOY
Release: 2016-06-06 20:52:44
Original
1080 people have browsed it

简单封装了一个MySql的类

class MySql{
    private $dbCon;
    public function __construct(){
        $this->dbCon=mysql_connect(...);
        mysql_select_db("test",$this->dbCon);
    }
    public function query($query){
        return mysql_query($query,$this->dbCon);
    }
    public function closeDb(){
        mysql_close($this->dbCon);
    }
}
Copy after login
Copy after login

另外在两个类里面实例化该MySql连接并且定义方法获取数据。

class UserDao{
    private $mysql;
    public function __construct(){
       $this->mysql=new MySql();
    }
    public function getUsers(){
      $query="SELECT * FROM USERS";
      $rs=$this->mysql->query($query);
      #format data
      return $result;
    }
    public function __destruct(){
        $this->mysql->closeDb();
    }
}
Copy after login
Copy after login
class NoteDao{
    private $mysql;
    public function __construct(){
       $this->mysql=new MySql();
    }
    public function getNotes(){
      $query="SELECT * FROM Notes";
      $rs=$this->mysql->query($query);
      #format data
      return $result;
    }
    public function __destruct(){
        $this->mysql->closeDb();
    }
}
Copy after login
Copy after login

然后新建一个test.php页面
如果单独引用任何一个UserDao或者NoteDao都没有问题。但是如果同时引用UserDao和NoteDao来获取数据,则会有
PHP Warning: mysql_close(): 9 is not a valid MySQL-Link resource in 。。。
这样的错误。
为什么呢?

回复内容:

简单封装了一个MySql的类

class MySql{
    private $dbCon;
    public function __construct(){
        $this->dbCon=mysql_connect(...);
        mysql_select_db("test",$this->dbCon);
    }
    public function query($query){
        return mysql_query($query,$this->dbCon);
    }
    public function closeDb(){
        mysql_close($this->dbCon);
    }
}
Copy after login
Copy after login

另外在两个类里面实例化该MySql连接并且定义方法获取数据。

class UserDao{
    private $mysql;
    public function __construct(){
       $this->mysql=new MySql();
    }
    public function getUsers(){
      $query="SELECT * FROM USERS";
      $rs=$this->mysql->query($query);
      #format data
      return $result;
    }
    public function __destruct(){
        $this->mysql->closeDb();
    }
}
Copy after login
Copy after login
class NoteDao{
    private $mysql;
    public function __construct(){
       $this->mysql=new MySql();
    }
    public function getNotes(){
      $query="SELECT * FROM Notes";
      $rs=$this->mysql->query($query);
      #format data
      return $result;
    }
    public function __destruct(){
        $this->mysql->closeDb();
    }
}
Copy after login
Copy after login

然后新建一个test.php页面
如果单独引用任何一个UserDao或者NoteDao都没有问题。但是如果同时引用UserDao和NoteDao来获取数据,则会有
PHP Warning: mysql_close(): 9 is not a valid MySQL-Link resource in 。。。
这样的错误。
为什么呢?

mysql_connect是会复用连接的。所以你new出来的两个 MySql 是用的同一个连接(可以var_dump出来看,resource id是同一个)。
mysql_connect第四个参数是是否新建连接,默认FALSE,置成 TRUE 就行了。

$this->dbCon = mysql_connect($host, $user, $passwd, TRUE);
Copy after login

但是我不太建议这么做,本来连接是可以复用的~~~没必要新建。用mysql_connect创建的连接php是会自动释放的,不用调用mysql_colse去关。

代码写错了

$dbCon=mysql_connect(...);
Copy after login

应该是

$this->dbCon=mysql_connect(...);
Copy after login
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