批改狀態(tài):合格
老師批語(yǔ):這個(gè)案例非常的實(shí)用, 完全 可以用在你自己的項(xiàng)目中
我們要實(shí)現(xiàn)增刪改查的數(shù)據(jù)庫(kù)原列表如下:
<?php namespace _2019C; // 定義一個(gè)接口iJie, 實(shí)現(xiàn)數(shù)據(jù)庫(kù)操作 interface iJie { public function create($data); // 增加數(shù)據(jù) public function read(); // 讀取數(shù)據(jù) public function update($data, $where); // 更新數(shù)據(jù) public function delete($where); // 刪除數(shù)據(jù) } // 創(chuàng)建類(lèi) Db class Db implements iJie { // 數(shù)據(jù)庫(kù)的連接對(duì)象,給一個(gè)空值防止出錯(cuò) protected $pdo = null; protected $table; // 構(gòu)造方法: 連接數(shù)據(jù)庫(kù),并設(shè)置默認(rèn)的數(shù)據(jù)表 public function __construct($dsn, $username, $password, $table) { $this->pdo = new \PDO($dsn, $username, $password); $this->table = $table; } // 增加數(shù)據(jù) public function create($data) { // 字段列表,表中有name, age, sex, mobile 字段 $fields = ' (name, age, sex, mobile) '; $values = ' (:name, :age, :sex, :mobile) '; // 創(chuàng)建數(shù)據(jù)庫(kù)SQL語(yǔ)句 $sql = 'INSERT INTO '.$this->table . $fields . ' VALUES ' . $values; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return [ 'count' => $stmt->rowCount(), 'id' => $this->pdo->lastInsertId() ]; } // 讀取數(shù)據(jù) ,查詢(xún)操作 public function read($fileds = '*' , $where='', $limit = '0, 5') { $where = empty($where) ? '' : ' WHERE ' . $where; $limit = ' LIMIT ' . $limit; $sql = 'SELECT '. $fileds . ' FROM ' . $this->table. $where . $limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); // 用二維數(shù)組返回所有數(shù)據(jù) return $stmt->fetchAll(\PDO::FETCH_ASSOC); } // 更新數(shù)據(jù) public function update($data, $where) { // 設(shè)置SET參數(shù) $keyArr = array_keys($data); $set = ''; // 遍歷數(shù)據(jù)庫(kù)字段列表, foreach ($keyArr as $value) { $set .= $value . '= :' . $value. ','; } //把最后一個(gè)逗號(hào)刪除, $set =rtrim($set, ', '); $sql = 'UPDATE '. $this->table.' SET '.$set.' WHERE '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); // 返回更新的數(shù)量 return $stmt->rowCount(); } // 刪除數(shù)據(jù) public function delete($where) { $sql = 'DELETE FROM '. $this->table. ' WHERE '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } } // 實(shí)例化Db $dsn = 'mysql:host=127.0.0.1;dbname=php'; $username = 'root'; $password = 'root'; $table = 'stud'; $db = new Db($dsn, $username, $password, $table); // 新增操作 $data = [ 'name'=> '狗子', 'age'=> 30, 'sex' => 1, 'mobile'=> '13888888888' ]; echo '<hr>'; foreach ($db->read() as $item) { print_r($item); echo '<br>'; } echo '<hr>'; // 查詢(xún)操作 foreach ($db->read('stud_id, name, age', 'age > 18') as $item) { print_r($item); echo '<br>'; } // 更新操作 $data = [ 'name'=> '狗子', 'age'=> 88, 'sex' => 1, 'mobile'=> '13000000000' ]; $where = 'stud_id = 16'; echo '成功的更新了: ' . $db->update($data, $where) . ' 條記錄'; echo '<hr>'; // 刪除操作,這里就不執(zhí)行了 //$where = 'stud_id = 16'; //echo '成功的刪除了: ' . $db->delete($where) . ' 條記錄';
刪除操作也是如此,下面是更新成功后的數(shù)據(jù)庫(kù)截圖:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)