批改狀態(tài):未批改
老師批語:
用接口跟類中的方法現(xiàn)實數(shù)據(jù)庫的增刪改查:
<?php //接口 interface iCurd { //增加 public function insert($data); //刪除 public function delete($where); //修改 public function update($data,$where); //查詢 public function select(); } class Db implements iCurd { //連接數(shù)據(jù)庫對象 protected $pdo; //數(shù)據(jù)庫表名 protected $table; //構(gòu)造方法 public function __construct($dsn,$user,$password,$table = 'staff') { //創(chuàng)建pdo連接對象 $this->pdo = new PDO($dsn,$user,$password); $this->table = $table; } //讀取操作方法 public function select($field = '*',$where = '',$limit = '1,5') { //設(shè)置查詢條件 $where = empty($where) ? '' : ' WHERE '.$where; //設(shè)置顯示 $limit = ' LIMIT '.$limit; //創(chuàng)建SQL預(yù)處理 $sql = 'SELECT '.$field. ' FROM '.$this->table.$where.$limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); //用fetchAll獲取數(shù)據(jù)庫數(shù)據(jù),并return返回 return $stmt->fetchAll(PDO::FETCH_ASSOC); } //新增操作方法 public function insert($data) { //SQL新增語句 //INSERT INTO `表名` (字段列表)VALUE (值列表) //字段列表 $fields = ' (name,age,sex,position,mobile,hiredate)'; //值列表 $values = ' (:name,:age,:sex,:position,:mobile,:hiredate)'; //創(chuàng)建SQL預(yù)處理 $sql = 'INSERT INTO '.$this->table.$fields.' VALUE '.$values; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); //返回新增數(shù)量,新增記錄的id組成的數(shù)據(jù) return [ 'count'=>$stmt->rowCount(), 'id'=>$this->pdo->lastInsertId() ]; } //更新,為了數(shù)據(jù)安全,不允許無條件更新 public function update($data, $where) { // 難點在于SET 參數(shù)的處理上,利用傳入$data數(shù)組,進(jìn)行拆裝 //獲取數(shù)組的鍵名組成的數(shù)組 //array_keys():獲取數(shù)組中的鍵名 $keyArr = array_keys($data); $set = ''; //遍歷鍵名表示的字段列表,拼裝預(yù)處理需要的SQL語句,注意占位符的表示 //遍歷獲取鍵名數(shù)組的中的值:就是$data中的鍵名 foreach ($keyArr as $value) { $set .= $value . ' = :' .$value. ', '; } //去掉最后一個逗號,注意每個逗號后面有一個空格,去除時也要帶上這個空格 //rtrim(第一個值為要清除的對象,第二個中值為要清除的字符):清楚指定的字符 $set = rtrim($set,', '); //預(yù)處理執(zhí)行更新操作 $sql = 'UPDATE '.$this->table.' SET '.$set.' WHERE '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); //返回被更新的記錄數(shù)量 return $stmt->rowCount(); } //刪除:與更新一樣,這也是危險操作,不允許無條件刪除 public function delete($where) { //預(yù)處理執(zhí)行刪除操作 $sql = 'DELETE FROM '.$this->table.' WHERE '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute(); //返回被刪除的記錄數(shù)量 return $stmt->rowCount(); } } //實例化Db類 $dsn = 'mysql:host=127.0.0.1;dbname=php'; $name = 'root'; $password = 'root'; $db = new Db($dsn,$name,$password); //遍歷讀取 foreach ($db->select() as $item) { print_r($item); echo '<br>'; } echo '<hr>'; //設(shè)置查詢條件 foreach ($db->select('staff_id, name, position','age > 50')as $item) { print_r($item); echo '<br>'; } echo'<hr>'; //新增數(shù)據(jù),$data是一個數(shù)組, //在類的方法中已經(jīng)設(shè)置好了字段列表和值列表模板, //在$data中填入需要添加的字段和對應(yīng)的值即可 $data = [ 'name'=>'郭靖', 'age'=>30, 'sex'=>1, 'position'=>'金刀駙馬', 'mobile'=>'13666668888', 'hiredate'=>time() ]; $res = $db->insert($data); echo '成功新增'.$res['count'].'條記錄,最新記錄的主鍵ID是: '.$res['id']; echo '<hr>'; //更新操作,更新剛添加的:郭靖 $data = [ 'age' => 40, 'position' => '抗金英雄' ]; //添加更新條件 $where = 'staff_id = 11'; echo '成功更新了:'. $db->update($data,$where).' 條記錄'; echo '<hr>'; //刪除記錄 //添加刪除條件 $where = 'staff_id = 11'; echo '成功刪除了:'.$db->delete($where).' 條記錄';
點擊 "運行實例" 按鈕查看在線實例
最終在頁面輸出數(shù)據(jù)庫的增刪改查結(jié)果:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號