Connect-phpmysql connection problem. Can't two connections be generated at the same time.

Source: Internet
Author: User
A MySql class {code...} is encapsulated, And the MySql connection is instantiated in the two classes and the data is obtained by defining methods. {Code...} and then create a new test. php page. If you reference any UserDao or NoteDao separately, there is no problem. However, if a MySql class is encapsulated at the same time...

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);    }}

In addition, the MySql connection is instantiated in two classes and the method is defined to obtain data.

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();    }}
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();    }}

Create a test. php page
If you reference any UserDao or NoteDao separately, there is no problem. However, if both UserDao and NoteDao are referenced to obtain data
PHP Warning: mysql_close (): 9 is not a valid MySQL-Link resource in...
This error occurs.
Why?

Reply content:

A MySql class is encapsulated.

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);    }}

In addition, the MySql connection is instantiated in two classes and the method is defined to obtain data.

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();    }}
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();    }}

Create a test. php page
If you reference any UserDao or NoteDao separately, there is no problem. However, if both UserDao and NoteDao are referenced to obtain data
PHP Warning: mysql_close (): 9 is not a valid MySQL-Link resource in...
This error occurs.
Why?

Mysql_connect will reuse the connection. Therefore, the two new MySql instances use the same connection (you can see var_dump that the resource id is the same ).
The fourth parameter of mysql_connect is whether to create a new connection. The default value is FALSE. Set this parameter to TRUE.

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

However, I do not recommend that you do this. connections can be reused ~~~ No need to create. The connection to php created with mysql_connect is automatically released without calling mysql_colse.

Code written incorrectly

$dbCon=mysql_connect(...);

It should be

$this->dbCon=mysql_connect(...);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.