[Php] uses php constructor and destructor to compile the Mysql DataBase Query Class and constructor mysql

Source: Internet
Author: User

[Php] uses php constructor and destructor to compile the Mysql DataBase Query Class and constructor mysql

In the previous article "[php] uses the original JavaScript Ajax for MVC hierarchical design for php and is compatible with IE6" (click to open the link), I used php to query the Mysql database model. php is not well written. In each method, you need to declare the $ con object of mysql and disable the $ con object of mysql. In this way, if there are more than one query method, a lot of code is added for declaring $ con objects and disabling $ con objects for no reason. In fact, php constructor and destructor can be used to inject $ con objects to various query methods of the database class, and $ con objects can be automatically recycled after each query.

Let's take an example to illustrate this problem. First, our task is very simple, that is, to query the testtable table of the test Database in mysql in descending order of the date time class to the web page.

For example:


First, we compile a model. php as follows,

First declare a private class member $ con as the global variable of this class.

You can put the database connection code in the constructor _ construct () of the Database Query Class testtable, and put the code that closes the database connection in the Destructor _ destruct, among them, _ construct () and _ destruct () are reserved keywords of function names in php, that is, once the two functions are declared, they are considered constructor and destructor.

It is worth noting that $ con, a declared private class member, must be assigned a value in the form of $ this-> con. $ Con = xx cannot be used directly. If it is $ con = xx, php considers that this variable is only effective within the current function and cannot act on the entire class.

The constructor requires a variable $ databaseName, which is the name of the database to be queried by the caller.

<? Phpclass testtable {private $ con; function _ construct ($ databaseName) {$ this-> con = mysql_connect ("localhost", "root", "root"); if (! $ This-> con) {die ("connection failed! ");} 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); }}?>

After completing the query, the getAll () method of the database query class does not need to declare the database connection or close the database connection. You can directly query the method and retrieve the Destructor after the query.

In controller. php, first introduce the testtable Query Class and call the getAll () method. The result is as follows:

<?phpheader("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>";?>

Related Article

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.