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