php數(shù)據(jù)庫操作model類(使用__call方法)
本文實(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è)計(jì)技巧總結(jié)》、《php+mssql數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php+redis數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Zend Studio (eclipse)使用速度優(yōu)化方法
Zend studio7.12那速度正太讓人火大了,修改文件的保存就building workspace,要得等上好一會(huì)2011-03-03怎么在Windows系統(tǒng)中搭建php環(huán)境
因?yàn)楹芏鄍hp初學(xué)者在環(huán)境搭建這一環(huán)節(jié)就費(fèi)了很大的功夫,導(dǎo)致失去了學(xué)習(xí)php的興趣,所以小編推薦初學(xué)者使用集成軟件搭建環(huán)境,隨著學(xué)習(xí)的不斷深入,再回過頭來了解環(huán)境2013-08-08PHP實(shí)現(xiàn)C#山寨ArrayList的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)C#山寨ArrayList的方法,通過一個(gè)php自定義類模擬實(shí)現(xiàn)C#中ArrayList的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07PHP與JavaScript針對Cookie的讀寫、交互操作方法詳解
這篇文章主要介紹了PHP與JavaScript針對Cookie的讀寫、交互操作方法,結(jié)合實(shí)例形式分析了php與javascript設(shè)置cookie、php讀取php及js設(shè)置的cookie、js讀取php及js設(shè)置的cookie等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08基于命令行執(zhí)行帶參數(shù)的php腳本并取得參數(shù)的方法
這篇文章主要介紹了基于命令行執(zhí)行帶參數(shù)的php腳本并取得參數(shù)的方法,分析了命令行運(yùn)行PHP代碼的原理、用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01PHP 魔術(shù)變量和魔術(shù)函數(shù)詳解
這篇文章主要簡單介紹了PHP 魔術(shù)變量和魔術(shù)函數(shù),以及使用示例,方便我們學(xué)習(xí)理解php魔術(shù)變量和魔術(shù)函數(shù),有需要的小伙伴參考下吧。2015-02-02