php增刪改查封裝
首先創(chuàng)建一個(gè)名為“DB”的類(lèi);然后在“DB”類(lèi)的構(gòu)造方法中去連接數(shù)據(jù)庫(kù);再根據(jù)連接實(shí)例進(jìn)行編寫(xiě)增刪改查操作;最后將“DB”類(lèi)進(jìn)行實(shí)例化并設(shè)置為全局變量即可。
封裝代碼:
<?php /** * 數(shù)據(jù)庫(kù)配置信息 */ define('DB_HOST','127.0.0.1'); //服務(wù)器 define('DB_USER','root'); //數(shù)據(jù)庫(kù)用戶(hù)名 define('DB_PASSWORD','123456'); //數(shù)據(jù)庫(kù)密碼 define('DB_NAME','TEST'); //默認(rèn)數(shù)據(jù)庫(kù) define('DB_CHARSET','utf8'); //數(shù)據(jù)庫(kù)字符集 define('TIMEZONE',"PRC"); //時(shí)區(qū)設(shè)置 date_default_timezone_set(TIMEZONE); /** * 類(lèi)名:DB * 說(shuō)明:數(shù)據(jù)庫(kù)操作類(lèi) */ class DB { public $host; //服務(wù)器 public $conn; //數(shù)據(jù)庫(kù)連接變量 /** * DB類(lèi)構(gòu)造函數(shù) */ public function DB($host=DB_HOST ,$username=DB_USER,$password=DB_PASSWORD,$db_name=DB_NAME) { $this->host = $host; $this->username = $username; $this->password = $password; $this->db_name = $db_name; $this->conn = mysql_connect($host,$username,$password) or die ('數(shù)據(jù)庫(kù)連接失?。″e(cuò)誤原因:'.mysql_error()); mysql_select_db($db_name)or die('數(shù)據(jù)庫(kù)選定失??!錯(cuò)誤原因:'.mysql_error()); mysql_query("SET CHARACTER SET utf8"); } /** * 關(guān)閉數(shù)據(jù)連接 */ public function close() { mysql_close($this->conn); } /** * @description調(diào)用方法用 * @param $sql * @return array */ public function QueryAll($sql) { $this->open(); $rs = mysql_query($sql,$this->conn); $objList = array(); while($obj = mysql_fetch_object($rs)) { if($obj) { $objList[] = $obj; } } $this->close(); return $objList; } /** * description查詢(xún)?nèi)糠祷豃son格式,通訊用 * @param $sql * @return string */ public function QueryAllJson($sql) { echo $sql; $this->open(); $rs = mysql_query($sql,$this->conn); $objList = array(); $i=0; while($obj = mysql_fetch_object($rs)) { $objList[$i]=$obj; $i++; } $this->close(); return json_encode(array("result"=>"success",'data'=>$objList)); } /** * @description 插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中 * @param $tableName 表名 * @param array $columns 包含表中所有字段名的數(shù)組。默認(rèn)空數(shù)組,則是全部有序字段名 * @param array $values 包含對(duì)應(yīng)所有字段的屬性值的數(shù)組 * @return int */ public function insertData($tableName,$columns=array(),$values=array()) { $sql = 'insert into '.$tableName .'( '; for($i = 0; $i < sizeof($columns);$i ++) { $sql .= $columns[$i]; if($i < sizeof($columns) - 1) { $sql .= ','; } } $sql .= ') values ( '; for($i = 0; $i < sizeof($values);$i ++) { $sql .= "'".$values[$i]."'"; if($i < sizeof($values) - 1) { $sql .= ','; } } $sql .= ' )'; $this->open(); mysql_query($sql,$this->conn); return true; // $id = mysql_insert_id($this->conn);//取得上一步操作產(chǎn)生的ID(2) // $this->close();(2) // return $id;//(2) } /** * 通過(guò)表中的某一屬性獲取數(shù)據(jù) */ public function getDataByAtr($tableName,$atrName,$atrValue){ @$data = $this->QueryAll("SELECT * FROM ".$tableName." WHERE $atrName = '$atrValue'"); if(count($data)!=0)return $data; return NULL; } /** * description_ * 通過(guò)表中的"id",刪除記錄 */ public function delete($tableName,$atrName,$atrValue){ echo $tableName; $this->open(); $deleteResult = false; if(mysql_query("DELETE FROM ".$tableName." WHERE $atrName = '$atrValue'")) $deleteResult = true; $this->close(); if($deleteResult) return true; else return false; } /** * 更新表中的屬性值 */ public function updateParamById($tableName,$atrName,$atrValue,$key,$value){ $db = new DB(); $db->open(); if(mysql_query("UPDATE ".$tableName." SET $key = '$value' WHERE $atrName = '$atrValue' ")){ //$key不要單引號(hào) $db->close(); return true; } else{ $db->close(); return false; } } /* * @description: 取得一個(gè)table的所有屬性名 * @param: $tbName 表名 * @return:字符串?dāng)?shù)組 */ public function fieldName($tableName){ $resultName=array(); $i=0; $this->open(); $result = mysql_query("SELECT * FROM ".$tableName); while ($property = mysql_fetch_field($result)){ $resultName[$i++]=$property->name; } $this->close(); return $resultName; } }
推薦教程:《PHP教程》