相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說(shuō)明
- thinkPHP5框架實(shí)現(xiàn)分頁(yè)查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫(xiě)文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
php mysql數(shù)據(jù)庫(kù)操作類(實(shí)例講解)
接著稍微說(shuō)說(shuō)整體的思路。整個(gè)類的封裝,包含一個(gè)連接數(shù)據(jù)庫(kù)的私有屬性$conn和若干操作函數(shù)。$conn在對(duì)象實(shí)例化的時(shí)候,由構(gòu)造函數(shù)處理傳入的參數(shù)后返回一個(gè)資源型的連接句柄。而后即可通過(guò)調(diào)用該實(shí)例化的對(duì)象的相應(yīng)方法對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪查改的操作。
talk less and show code:
<?php /** *以下代碼用于數(shù)據(jù)庫(kù)操作類的封裝 * * @author rex<rex.sp.li@aliyun.com> * @version 1.0 * @since 2015 */ class Mysql{ //數(shù)據(jù)庫(kù)連接返回值 private $conn; /** * [構(gòu)造函數(shù),返回值給$conn] * @param [string] $hostname [主機(jī)名] * @param [string] $username[用戶名] * @param [string] $password[密碼] * @param [string] $dbname[數(shù)據(jù)庫(kù)名] * @param [string] $charset[字符集] * @return [null] */ function __construct($hostname,$username,$password,$dbname,$charset='utf8'){ $conn = @mysql_connect($hostname,$username,$password); if(!$conn){ echo '連接失敗,請(qǐng)聯(lián)系管理員'; exit; } $this->conn = $conn; $res = mysql_select_db($dbname); if(!$res){ echo '連接失敗,請(qǐng)聯(lián)系管理員'; exit; } mysql_set_charset($charset); } function __destruct(){ mysql_close(); } /** * [getAll 獲取所有信息] * @param [string] $sql [sql語(yǔ)句] * @return [array] [返回二維數(shù)組] */ function getAll($sql){ $result = mysql_query($sql,$this->conn); $data = array(); if($result && mysql_num_rows($result)>0){ while($row = mysql_fetch_assoc($result)){ $data[] = $row; } } return $data; } /** * [getOne 獲取單條數(shù)據(jù)] * @param [string] $sql [sql語(yǔ)句] * @return [array] [返回一維數(shù)組] */ function getOne($sql){ $result = mysql_query($sql,$this->conn); $data = array(); if($result && mysql_num_rows($result)>0){ $data = mysql_fetch_assoc($result); } return $data; } /** * [getOne 獲取單條數(shù)據(jù)] * @param [string] $table [表名] * @param [string] $data [由字段名當(dāng)鍵,屬性當(dāng)鍵值的一維數(shù)組] * @return [type] [返回false或者插入數(shù)據(jù)的id] */ function insert($table,$data){ $str = ''; $str .="INSERT INTO `$table` "; $str .="(`".implode("`,`",array_keys($data))."`) "; $str .=" VALUES "; $str .= "('".implode("','",$data)."')"; $res = mysql_query($str,$this->conn); if($res && mysql_affected_rows()>0){ return mysql_insert_id(); }else{ return false; } } /** * [update 更新數(shù)據(jù)庫(kù)] * @param [string] $table [表名] * @param [array] $data [更新的數(shù)據(jù),由字段名當(dāng)鍵,屬性當(dāng)鍵值的一維數(shù)組] * @param [string] $where [條件,‘字段名'=‘字段屬性'] * @return [type] [更新成功返回影響的行數(shù),更新失敗返回false] */ function update($table,$data,$where){ $sql = 'UPDATE '.$table.' SET '; foreach($data as $key => $value){ $sql .= "`{$key}`='{$value}',"; } $sql = rtrim($sql,','); $sql .= " WHERE $where"; $res = mysql_query($sql,$this->conn); if($res && mysql_affected_rows()){ return mysql_affected_rows(); }else{ return false; } } /** * [delete 刪除數(shù)據(jù)] * @param [string] $table [表名] * @param [string] $where [條件,‘字段名'=‘字段屬性'] * @return [type] [成功返回影響的行數(shù),失敗返回false] */ function del($table,$where){ $sql = "DELETE FROM `{$table}` WHERE {$where}"; $res = mysql_query($sql,$this->conn); if($res && mysql_affected_rows()){ return mysql_affected_rows(); }else{ return false; } } }