Home >Backend Development >PHP Tutorial >Use PHP's constructor and destructor to write sample code for Mysql database query class
For phpQuery Mysql dataThe model.php writing method of the library is not perfect enough. In each method, you need to declare the $con object of mysql yourself and close the $con object of mysql yourself. In this way, if there are too many query methods, a lot of code for declaring $con objects and closing $con objects will be added for no reason. In fact, you can completely use PHP's constructor and destructor to inject $con objects into each query method of the database class, and at the same time automatically recycle the $con object after each query.
Let's give an example to illustrate this problem. First of all, our task is very simple, which is to query the testtable table of the test database in mysql and sort the results in descending order by date and time to the web page.
As shown below:
First, we write a model.php as follows,
First declare a private class member $con serves as a global variable of this class.
You can put the code to establish the database connection in the constructor construct() of the testtable database query class, and put the code to close the database connection in the destructor In the number destruct(), construct() and destruct() are reserved keywords for function names in PHP. That is, once these two functions are declared, They are considered constructors and destructors.
It is worth noting that when assigning a value to the declared private class member $con, you must use the form $this->con. You cannot directly use $con=xx. If it is $con=xx, PHP considers that the scope of this variable is only within the current function and cannot affect the entire class.
The constructor requires a variable $databaseName, which is the name of the database that the caller needs to query.
<?php class testtable{ private $con; function construct($databaseName){ $this->con=mysql_connect("localhost","root","root"); if(!$this->con){ die("连接失败!"); } mysql_select_db($databaseName,$this->con); mysql_query("set names utf8;"); } public function getAll(){ $result=mysql_query("select * from testtable order by date desc;"); $testtableList=array(); for($i=0;$row=mysql_fetch_array($result);$i++){ $testtableList[$i]['id']=$row['id']; $testtableList[$i]['username']=$row['username']; $testtableList[$i]['number']=$row['number']; $testtableList[$i]['date']=$row['date']; } return $testtableList; } function destruct(){ mysql_close($this->con); } } ?>
<?php header("Content-type: text/html; charset=utf-8"); include_once("model.php"); $testtable=new testtable("test"); $testtableList=$testtable->getAll(); echo "<table>"; for($i=0;$i<count($testtableList);$i++){ echo "<tr> <td>".$testtableList[$i]['id']."</td> <td>".$testtableList[$i]['username']."</td> <td>".$testtableList[$i]['number']."</td> <td>".$testtableList[$i]['date']."</td> </tr>"; } echo "</table>"; ?>
The above is the detailed content of Use PHP's constructor and destructor to write sample code for Mysql database query class. For more information, please follow other related articles on the PHP Chinese website!