PHP模型Model類封裝數(shù)據(jù)庫操作示例
本文實(shí)例講述了PHP模型Model類封裝數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:&l;?php//引入配置文件iclude&quo;./cofig.php&quo;;claModel{public$lik;//存儲連接對象public$ableNa......
以下是【金聰采編】分享的內(nèi)容全文:
以下是【金聰采編】分享的內(nèi)容全文:
本文實(shí)例講述了PHP模型Model類封裝數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
<?php //引入配置文件 include "./config.php"; class Model { public $link;//存儲連接對象 public $tableName = "";//存儲表名 public $field = "*";//存儲字段 public $allFields = [];//存儲當(dāng)前表所有字段 public $where = "";//存儲where條件 public $order = "";//存儲order條件 public $limit = "";//存儲limit條件 /** * 構(gòu)造方法 初始化 * @param string $tableName 要操作的表名 */ public function __construct($tableName) { //1.存儲操作的表名 $this->tableName = PRE.$tableName; //2.初始化連接數(shù)據(jù)庫 $this->getConnect(); //3.獲得當(dāng)前表的所有字段 $this->getFields(); } /** * 初始化連接數(shù)據(jù)庫操作 */ public function getConnect() { //1.連接數(shù)據(jù)庫 $this->link = mysqli_connect(HOST,USER,PWD,DB,PORT); //2.判斷連接 if (mysqli_connect_errno($this->link)>0){ echo mysqli_connect_error($this->link); exit; } } /** * 執(zhí)行并發(fā)送SQL(查詢) * @param string $sql 要查詢的SQL語句 * @return array 返回查詢出來的二維數(shù)組 */ public function query($sql) { $result = mysqli_query($this->link,$sql); if ($result && mysqli_num_rows($result)>0) { $arr = []; while($row = mysqli_fetch_assoc($result)){ $arr[] = $row; } } return $arr; } /** * 獲取當(dāng)前表的所有字段 */ public function getFields() { //查看表結(jié)構(gòu) $sql = "desc {$this->tableName}"; //執(zhí)行并發(fā)送SQL $result = $this->query($sql); $fields = []; foreach ($result as $k => $v){ $fields[] = $v['Field']; } $this->allFields = $fields; } /** * 執(zhí)行并發(fā)送SQL語句(增刪改) * @param string $sql 要執(zhí)行的SQL語句 * @return bool|int|string 添加成功則返回上一次操作id,刪除修改操作則返回true,失敗則返回false */ public function exec($sql) { $result = mysqli_query($this->link,$sql); //處理結(jié)果集 if ($result && mysqli_affected_rows($this->link)>0){ //判斷是否為添加操作,是則返回上一次執(zhí)行的id if (mysqli_insert_id($this->link)){ return mysqli_insert_id($this->link); } //刪除修改操作成功則返回true return true; }else{ //未執(zhí)行成功則返回false return false; } } /** * 查詢多條數(shù)據(jù) */ public function select() { $sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}"; //執(zhí)行并發(fā)送SQL return $this->query($sql); } /** * 查詢一條數(shù)據(jù) * @param string $id 要查詢的id * @return array 返回一條數(shù)據(jù) */ public function find($id="") { //判斷id是否存在 if (empty($id)){ $where = $this->where; }else{ $where = "where id={$id}"; } $sql = "select {$this->field} from {$this->tableName} {$where} limit 1"; //執(zhí)行并發(fā)送sql $result = $this->query($sql); //返回一條數(shù)據(jù) return $result[0]; } /** * 設(shè)置要查詢的字段信息 * @param string $field 要查詢的字段 * @return object 返回自己,保證連貫操作 */ public function field($field) { //判斷字段是否存在 if (empty($field)){ return $this; } $this->field = $field; return $this; } /** * 統(tǒng)計(jì)總條數(shù) * @return int 返回總數(shù) */ public function count() { //準(zhǔn)備SQL語句 $sql = "select count(*) as total from {$this->tableName} limit 1"; $result = $this->query($sql); //返回總數(shù) return $result[0]['total']; } /** * 添加操作 * @param array $data 要添加的數(shù)組 * @return bool|int|string 添加成功則返回上一次操作的id,失敗則返回false */ public function add($data){ //判斷是否是數(shù)組 if (!is_array($data)){ return $this; } //判斷是否全是非法字段 if (empty($data)){ die("非法數(shù)據(jù)"); } //過濾非法字段 foreach ($data as $k => $v){ if (!in_array($k,$this->allFields)){ unset($data[$k]); } } //將數(shù)組中的鍵取出 $keys = array_keys($data); //將數(shù)組中取出的鍵轉(zhuǎn)為字符串拼接 $key = implode(",",$keys); //將數(shù)組中的值轉(zhuǎn)化為字符串拼接 $value = implode("','",$data); //準(zhǔn)備SQL語句 $sql = "insert into {$this->tableName} ({$key}) values('{$value}')"; //執(zhí)行并發(fā)送SQL return $this->exec($sql); } /** * 刪除操作 * @param string $id 要?jiǎng)h除的id * @return bool 刪除成功則返回true,失敗則返回false */ public function delete($id="") { //判斷id是否存在 if (empty($id)){ $where = $this->where; }else{ $where = "where id={$id}"; } $sql = "delete from {$this->tableName} {$where}"; echo $sql; //執(zhí)行并發(fā)送 return $this->exec($sql); } /** * 修改操作 * @param array $data 要修改的數(shù)組 * @return bool 修改成功返回true,失敗返回false */ public function update($data){ //判斷是否是數(shù)組 if (!is_array($data)){ return $this; } //判斷是否是全是非法字段 if(empty($data)){ die("非法數(shù)據(jù)"); } $str = ""; //過濾非法字段 foreach ($data as $k => $v){ if ($k == "id"){ $where = "where id={$v}"; unset($data[$k]); } if (in_array($k,$this->allFields)){ $str .= "{$k}='{$v}',"; }else{ unset($data[$k]); } } //判斷是否有條件 if (empty($this->where)){ die("請輸入條件"); } //去掉最右側(cè)的逗號 $str = rtrim($str,","); $sql = "update {$this->tableName} set {$str} {$this->where}"; return $this->exec($sql); } /** * where條件 * @param string $where 輸入的where條件 * @return $this 返回自己,保證連貫操作 */ public function where($where) { $this->where = "where ".$where; return $this; } /** * order排序條件 * @param string $order 以此為基準(zhǔn)進(jìn)行排序 * @return $this 返回自己,保證連貫操作 */ public function order($order) { $this->order = "order by ".$order; return $this; } /** * limit條件 * @param string $limit 輸入的limit條件 * @return $this 返回自己,保證連貫操作 */ public function limit($limit) { $this->limit = "limit ".$limit; return $this; } /** * 析構(gòu)方法 * 關(guān)閉數(shù)據(jù)庫連接 */ public function __destruct() { mysqli_close($this->link); } } //自行調(diào)試 $a = new Model("表名"); // var_dump($a->find(3)); // var_dump($a->select()); // var_dump($a->count()); // $res = $a->select(); //var_dump($res);?>更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
金聰線報(bào)提示:[ PHP模型Model類封裝數(shù)據(jù)庫操作示例 ] 僅為會員分享,分享目的如下:
1.軟件源碼推廣展示:目的展示軟件相關(guān)功能,接收技術(shù)學(xué)習(xí)者測試、測評;
2.教程課程信息展示:展示課程信息,傳授課程各階段內(nèi)容;
3.設(shè)計(jì)素材圖片展示:展示素材設(shè)計(jì)理念、思維方式、傳播設(shè)計(jì)理念;
4.福利優(yōu)惠信息展示:分享各類最新的福利信息,各種優(yōu)惠信息展示;
以上分享目的僅供學(xué)習(xí)、參考使用,請勿用于其他用途,如果想商業(yè)使用或者代理,請自行聯(lián)系版權(quán)方獲取授權(quán)。任何未獲取授權(quán)的商業(yè)使用與本站無關(guān),請自行承擔(dān)相應(yīng)責(zé)任。
本站不存儲任何資源文件,敬請周知!
本網(wǎng)站采用 BY-NC-SA 協(xié)議進(jìn)行授權(quán) 轉(zhuǎn)載請注明原文鏈接:PHP模型Model類封裝數(shù)據(jù)庫操作示例
1.軟件源碼推廣展示:目的展示軟件相關(guān)功能,接收技術(shù)學(xué)習(xí)者測試、測評;
2.教程課程信息展示:展示課程信息,傳授課程各階段內(nèi)容;
3.設(shè)計(jì)素材圖片展示:展示素材設(shè)計(jì)理念、思維方式、傳播設(shè)計(jì)理念;
4.福利優(yōu)惠信息展示:分享各類最新的福利信息,各種優(yōu)惠信息展示;
以上分享目的僅供學(xué)習(xí)、參考使用,請勿用于其他用途,如果想商業(yè)使用或者代理,請自行聯(lián)系版權(quán)方獲取授權(quán)。任何未獲取授權(quán)的商業(yè)使用與本站無關(guān),請自行承擔(dān)相應(yīng)責(zé)任。
本站不存儲任何資源文件,敬請周知!
此資源僅供個(gè)人學(xué)習(xí)、研究使用,禁止非法轉(zhuǎn)播或商業(yè)用途,請?jiān)讷@取后24小時(shí)內(nèi)刪除,如果你覺得滿意,請尋求購買正版或獲取授權(quán)!
如果您認(rèn)為本頁信息內(nèi)容侵犯了您的相關(guān)權(quán)益(包含但不限于:著作權(quán)、首發(fā)權(quán)、隱私權(quán)等權(quán)利),或者您認(rèn)為自己是此信息的權(quán)利人但是此信息不是自己發(fā)布的,可以直接版權(quán)舉報(bào)投訴,我們會根據(jù)網(wǎng)站注冊協(xié)議、資源分享協(xié)議等協(xié)議處理,以保護(hù)您的合法權(quán)益。
免責(zé)申明:本站僅提供學(xué)習(xí)的平臺,所有資料均來自于網(wǎng)絡(luò)分享線索,版權(quán)歸原創(chuàng)者所有!本站不提供任何保證,并不承擔(dān)任何法律責(zé)任,如果對您的版權(quán)或者利益造成損害,請?zhí)峁┫鄳?yīng)的資質(zhì)證明,我們將于3個(gè)工作日內(nèi)予以處理。版權(quán)申訴相關(guān)說明如果您認(rèn)為本頁信息內(nèi)容侵犯了您的相關(guān)權(quán)益(包含但不限于:著作權(quán)、首發(fā)權(quán)、隱私權(quán)等權(quán)利),或者您認(rèn)為自己是此信息的權(quán)利人但是此信息不是自己發(fā)布的,可以直接版權(quán)舉報(bào)投訴,我們會根據(jù)網(wǎng)站注冊協(xié)議、資源分享協(xié)議等協(xié)議處理,以保護(hù)您的合法權(quán)益。
本網(wǎng)站采用 BY-NC-SA 協(xié)議進(jìn)行授權(quán) 轉(zhuǎn)載請注明原文鏈接:PHP模型Model類封裝數(shù)據(jù)庫操作示例

侵權(quán)舉報(bào)/版權(quán)申訴



