zoukankan      html  css  js  c++  java
  • 【php】利用php的构造函数与析构函数编写Mysql数据库查询类 (转)

    上次在《【php】利用原生态的JavaScript Ajax为php进行MVC分层设计,兼容IE6》(点击打开链接) 一文中,对于php查询Mysql数据库的model.php写法还不够完善,在每一个方法中还需要自己声明mysql的$con对象,同时自己关闭 mysql的$con对象。这样,如果查询方法一多,再无缘无故地增加了许多声明$con对象与关闭$con对象的代码。其实完全可以利用php的构造函 数与析构函数给数据库类各个查询方法的注入$con对象,同时自动在每次查询之后自动回收$con对象。

    直接举一个例子说明这个问题,首先我们的任务很简单,就是把mysql中test数据库的testtable表,按date时间类降序排序的结果查询到网页上。

    如下图:


    首先,我们编写一个model.php如下,

    先声明一个私有的类成员$con作为这个类的全局变量。

    可以把建立数据库连接的代码放在testtable这个数据库查询类的构造函数__construct()里面,把关闭数据库连接的代码放在析构函 数__destruct()里面,其中__construct()与__destruct()是php中的函数名保留关键字,也就是一旦声明这两个函数, 就被认为是构造函数与析构函数。

    值得注意的是,为声明的私有类成员$con赋值,必须用$this->con的形式。不可以直接$con=xx,如果是$con=xx,php认为这个变量的作用范围仅在当前函数内,不能作用于整个类。

    构造函数,要求一个变量$databaseName,此乃调用者需要查询的数据库名。

    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("连接失败!");    
    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. ?>    

    搞完之后,getAll()这个数据库查询类的查询方法,无须声明数据库连接与关闭数据库连接,直接就可以进行查询,查询完有析构函数进行回收。

    在controller.php中先引入这个testtable查询类,再进行getAll()方法的调用,则得到如上图的效果:

      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. ?> 
  • 相关阅读:
    回头再看libpcap
    lex error
    perl 的威力
    什么叫回流和重绘?
    HTTP协议与FTP协议的区别【转】
    Linux操作系统Redhat系列与Debian系列 【转】
    dock是什么? docker就是集装箱原理【转】
    光端机的作用【转】
    c++ -- call_once用法
    c++ -- thread详细解析
  • 原文地址:https://www.cnblogs.com/mafeng/p/5629450.html
Copyright © 2011-2022 走看看