国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

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

2024-05-04 23:52:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

該數(shù)據(jù)庫(kù)操作類使用__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ù)庫(kù)連接失敗";      }      $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"));

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 唐山市| 陇南市| 两当县| 金华市| 永登县| 古田县| 新兴县| 珠海市| 九龙坡区| 大石桥市| 齐齐哈尔市| 道真| 普兰店市| 凉城县| 利津县| 新田县| 乾安县| 大渡口区| 神木县| 五河县| 冕宁县| 山阳县| 盐亭县| 简阳市| 六盘水市| 苍溪县| 麻栗坡县| 平南县| 五大连池市| 吴川市| 绥江县| 浦江县| 巴里| 博乐市| 铁力市| 新宾| 海安县| 博爱县| 崇阳县| 郴州市| 大新县|