最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php數(shù)據(jù)庫操作model類(使用__call方法)

 更新時間:2016年11月16日 14:15:01   作者:shyhero  
這篇文章主要介紹了php數(shù)據(jù)庫操作model類,使用__call方法實(shí)現(xiàn)了數(shù)據(jù)的查詢功能,需要的朋友可以參考下

本文實(shí)例講述了php數(shù)據(jù)庫操作model類。分享給大家供大家參考,具體如下:

該數(shù)據(jù)庫操作類使用__call()方法實(shí)現(xiàn)了數(shù)據(jù)的查找功能。

代碼如下:

<?php
/*
作者 : shyhero
*/
define("HOSTNAME","127.0.0.1");
define("USERNAME","root");
define("PASSWORD","");
define("DATANAME","class");
class Model{
    private $link;
    private $tableName;
    private $zd;
    private $method = array(
      "where" => "",
      "order" => "",
      "limit" => "",
      "group" => "",
      "having" => ""
      );
    public function __construct($tableName){
      $this -> tableName = $tableName;
      try{
        $this -> link = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATANAME);
        mysqli_set_charset($this -> link,"UTF8");
      }catch(Exception $e){
        echo "數(shù)據(jù)庫連接失敗";
      }
      $this -> desc();
    }
    public function __destruct(){
      mysqli_close($this -> link);
    }
    public function desc(){
      $sql = " desc {$this -> tableName}; ";
      $res = mysqli_query($this -> link,$sql);
      $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
      for($i = 0 ;$i < count($arr);$i++){
        $brr[] = $arr[$i]['Field'];
      }
      $this -> zd = $brr;
      return $brr;
    }
    public function __call($name,$value){
      $name = strtolower($name);
      if(array_key_exists($name,$this -> method)){
        if($name == 'order'){
          $this -> method['order'] = " order by ".$value[0];
        }elseif($name == 'group'){
        $this -> method['group'] = " group by ".$value[0];
        }else{
          $this -> method[$name] = " {$name} ".$value[0];
        }
      }else{
        return "the method is not found!";
      }
      return $this;
    }
    public function method(){
      return " {$this -> method['where']} {$this -> method['order']} {$this -> method['limit']} {$this -> method['group']} {$this -> method['having']}; ";
    }
    public function find($a="*"){
      if(in_array("{$a}",$this -> zd) || $a == "*"){
        $sql = "select {$a} from {$this -> tableName} {$this -> method()} ";
      }else{
        $sql = "select * from {$this -> tableName}";
      }
      //return $sql;
      $res = mysqli_query($this -> link,$sql);
      $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
      return $arr;
    }
}

用法示例:

<?php
  function __autoload($className){
    require($className.".class.php");
  }
  $a = new Model("stu");
  $a -> where("name = 'zhu'")->limit("5,10");
  var_dump($a -> find("name"));

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php+mssql數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php+redis數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

  • PHP實(shí)現(xiàn)獲取圖片顏色值的方法

    PHP實(shí)現(xiàn)獲取圖片顏色值的方法

    這篇文章主要介紹了PHP實(shí)現(xiàn)獲取圖片顏色值的方法,需要的朋友可以參考下
    2014-07-07
  • PHP識別二維碼的方法(php-zbarcode安裝與使用)

    PHP識別二維碼的方法(php-zbarcode安裝與使用)

    這篇文章主要介紹了PHP識別二維碼的方法,通過安裝ImageMagick和php-zbarcode擴(kuò)展實(shí)現(xiàn)針對二維碼的識別功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • PHP 引用的概念

    PHP 引用的概念

    這篇文章主要介紹了PHP的中引用的概念,幫助大家更好的理解和學(xué)習(xí)使用PHP,感興趣的朋友可以了解下
    2021-04-04
  • PHP引用返回用法示例

    PHP引用返回用法示例

    這篇文章主要介紹了PHP引用返回的用法,結(jié)合實(shí)例形式分析了針對函數(shù)參數(shù)及函數(shù)的引用使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-05-05
  • php實(shí)現(xiàn)保存submit內(nèi)容之后禁止刷新

    php實(shí)現(xiàn)保存submit內(nèi)容之后禁止刷新

    這篇文章主要介紹了php保存submit內(nèi)容之后禁止刷新的具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-03-03
  • php實(shí)現(xiàn)基于pdo的事務(wù)處理方法示例

    php實(shí)現(xiàn)基于pdo的事務(wù)處理方法示例

    這篇文章主要介紹了php實(shí)現(xiàn)基于pdo的事務(wù)處理方法,結(jié)合實(shí)例形式分析了php使用pdo進(jìn)行事務(wù)操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • PHP 中的面向?qū)ο缶幊蹋和ㄏ虼笮?PHP 工程的辦法

    PHP 中的面向?qū)ο缶幊蹋和ㄏ虼笮?PHP 工程的辦法

    PHP 中的面向?qū)ο缶幊蹋和ㄏ虼笮?PHP 工程的辦法...
    2006-12-12
  • php 類中的常量、靜態(tài)屬性、非靜態(tài)屬性的區(qū)別

    php 類中的常量、靜態(tài)屬性、非靜態(tài)屬性的區(qū)別

    這篇文章主要介紹了php 類中的常量、靜態(tài)屬性、非靜態(tài)屬性的區(qū)別與介紹,需要的朋友可以參考下
    2017-04-04
  • php 更新數(shù)據(jù)庫中斷的解決方法

    php 更新數(shù)據(jù)庫中斷的解決方法

    使用PHP程序更新數(shù)據(jù)庫,總是到160000條時中斷, 查看LOG,顯示超過PHP最大執(zhí)行時間的錯誤, 只需要在PHP腳本中加入如下語句
    2009-06-06
  • 最新評論

    延寿县| 永新县| 巴楚县| 鲜城| 博白县| 兴安盟| 如皋市| 宁津县| 依兰县| 兴义市| 吉安县| 正镶白旗| 明水县| 中卫市| 云浮市| 富裕县| 威信县| 隆子县| 修武县| 自贡市| 琼中| 和平县| 白沙| 祁东县| 林西县| 桂东县| 凯里市| 锦屏县| 花莲市| 桦南县| 鸡东县| 绥芬河市| 紫金县| 临邑县| 天长市| 屏南县| 五华县| 潍坊市| 沧州市| 习水县| 淮北市|