Home >Backend Development >PHP Tutorial >Use PHP's constructor and destructor to write sample code for Mysql database query class

Use PHP's constructor and destructor to write sample code for Mysql database query class

黄舟
黄舟Original
2017-07-02 10:20:051430browse

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][&#39;id&#39;]=$row[&#39;id&#39;];  
            $testtableList[$i][&#39;username&#39;]=$row[&#39;username&#39;];    
            $testtableList[$i][&#39;number&#39;]=$row[&#39;number&#39;];    
            $testtableList[$i][&#39;date&#39;]=$row[&#39;date&#39;];    
        }    
        return $testtableList;    
    }  
    function destruct(){  
        mysql_close($this->con);    
    }  
}  
?>





## After that, the query method of getAll() database query class can be queried directly without declaring and closing the database connection. After the query is completed, the destructor will be used for recycling.

First introduce this testtable query class in controller.php, and then call the getAll() method, you will get the effect as shown above:

<?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][&#39;id&#39;]."</td>  
            <td>".$testtableList[$i][&#39;username&#39;]."</td>  
            <td>".$testtableList[$i][&#39;number&#39;]."</td>  
            <td>".$testtableList[$i][&#39;date&#39;]."</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!

    Statement:
    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