PHP uses PHP's constructors and destructors to write MySQL database query classes

Source: Internet
Author: User
Tags vars

Last time in "PHP" using the original Java JavaScript ajax for the MVC layered design, compatible with IE6 "(click Open link) in the article, For PHP query MySQL database model.php writing is not perfect, in each method also need to declare the MySQL $con object, and shut down the MySQL $con object. This way, if you query more than one method, you add a lot of code to declare $con objects and close $con objects for no apparent reason. In fact, the PHP constructor and destructor can be used to inject $con objects into the database class, and automatically reclaim $con objects after each query.

To give an example to illustrate this problem, first of all, our task is very simple, that is, the MySQL test database testtable table, sorted by the date time class descending order of the results query to the Web page.

Such as:


First, we write a model.php as follows,

Declare a private class member $con as a global variable for this class.

The code that establishes the database connection can be put in testtable this database query class constructor __construct (), put the code to close the database connection in the destructor __destruct (), where __construct () and __ Destruct () is a function name in PHP that retains the keyword, that is, once the two functions are declared, they are considered constructors and destructors.

It is important to note that $con is assigned to the private class member of the declaration, which must be in the form of $this->con. Can not directly $con=xx, if it is $con=xx,php that the scope of the variable is only within the current function, not the entire class.

constructor, which requires a variable $databasename, which is the name of the database that the caller needs to query.

[PHP]View Plaincopy print?
  1. <?php
  2. Class testtable{
  3. private $con;
  4. function __construct ($databaseName) {
  5. $this->con=mysql_connect ("localhost","root", "root");
  6. if (! $this->con) {
  7. Die ("Connection failed!    ");
  8. }
  9. mysql_select_db ($databaseName,$this->con);
  10. mysql_query ("set names UTF8;");
  11. }
  12. Public function GetAll () {
  13. $result =mysql_query ("SELECT * from TestTable ORDER by date desc;");
  14. $testtableList =Array ();
  15. For ($i =0; $row =mysql_fetch_array ($result); $i + +) {
  16. $testtableList [$i] [' id ']=$row [' id '];
  17. $testtableList [$i] ['username ']=$row [' username '];
  18. $testtableList [$i] [' Number ']=$row [' number '];
  19. $testtableList [$i] [' Date ' ]=$row [' Date '];
  20. }
  21. return $testtableList;
  22. }
  23. function __destruct () {
  24. Mysql_close ($this->con);
  25. }
  26. }
  27. ?>

After the completion, GETALL () This database query class Query method, do not need to declare the database connection and close database connection, directly can query, query the completion of a destructor for recycling.

This testtable query class is first introduced in controller.php, and then the GetAll () method is called, the effect is as follows:

[PHP]View Plaincopy print?
    1. <?php
    2. Header ("content-type:text/html;     Charset=utf-8 ");
    3. Include_once ("model.php");
    4. $testtable =new TestTable ("test");
    5. $testtableList =$testtable->getall ();
    6. echo "<table>";
    7. For ($i =0; $i <count ($testtableList); $i + +) {
    8. Echo
    9. "<tr>
    10. <td>". $testtableList [$i] [' ID ']." </td>
    11. <td>". $testtableList [$i] [' username ']." </td>
    12. <td>". $testtableList [$i] [' number ']." </td>
    13. <td>". $testtableList [$i] [' Date ']." </td>
    14. </tr> ";
    15. }
    16. echo "</table>";
    17. ?>

PHP uses PHP's constructors and destructors to write MySQL database query classes (GO)

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.