【php】使用mysqli完成的一個數據庫操作類
阿新 • • 發佈:2018-12-24
<?php class lib_mysqli { protected $mysqli; //mysqli例項物件 public $sql; //sql語句 protected $rs; //結果集 protected $query_num = 0; //執行次數 protected $fetch_mode = MYSQLI_ASSOC; //獲取模式 protected $cache; //快取類物件 protected $reload = false; //是否重新載入 protected $cache_mark = true; //快取標記 //建構函式:主要用來返回一個mysqli物件 public function __construct($dbhost, $dbuser, $dbpass, $dbname, $dbport) { $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport); if(mysqli_connect_errno()) { $this->mysqli = false; echo '<h2>'.mysqli_connect_error().'</h2>'; die(); } else { $this->mysqli->set_charset("utf8"); } } //快取類物件:檔案快取、memcache鍵值對快取 public function cache_obj($cache) { $this->cache = $cache; } //解構函式:主要用來釋放結果集和關閉資料庫連線 public function __destruct() { $this->free(); $this->close(); } //釋放結果集所佔資源 protected function free() { @$this->rs->free(); } //關閉資料庫連線 protected function close() { $this->mysqli->close(); } //獲取結果集 protected function fetch() { return $this->rs->fetch_array($this->fetch_mode); } //獲取查詢的sql語句 protected function get_query_sql($sql, $limit = null) { if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) { $sql .= " LIMIT " . $limit; } return $sql; } //從快取中獲取資料 protected function get_cache($sql,$method) { $cache_file = md5($sql.$method); $res = $this->cache->get($cache_file); if(!$res) { //如果快取檔案過期或不存在的話,返回false;如果快取檔案存在且未過期的話,則返回快取資料 $res = $this->$method($sql); //先從快取中取資料,如果快取中沒資料,則從資料庫中取資料 if($res && $this->cache_mark && !$this->reload) { $this->cache->set($cache_file, $res);//如果快取檔案過期或不存在的話,將重新將從資料庫中查詢的資料放入快取檔案 } } return $res; } //獲取查詢次數 public function query_num() { return $this->query_num; } //執行sql語句查詢 public function query($sql, $limit = null) { $sql = $this->get_query_sql($sql, $limit); $this->sql[] = $sql; $this->rs = $this->mysqli->query($sql); if (!$this->rs) { echo "<p>error: ".$this->mysqli->error."</p>"; echo "<p>sql: ".$sql."</p>"; die(); } else { $this->query_num++; return $this->rs; } } //返回單條記錄的單個欄位值 public function get_one($sql) { $this->query($sql, 1); $this->fetch_mode = MYSQLI_NUM; $row = $this->fetch(); $this->free(); return $row[0]; } //快取單個欄位 public function cache_one($sql, $reload = false) { $this->reload = $reload; $sql = $this->get_query_sql($sql, 1); return $this->get_cache($sql, 'get_one'); } //獲取單條記錄 public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) { $this->query($sql, 1); $this->fetch_mode = $fetch_mode; $row = $this->fetch(); $this->free(); return $row; } //快取行 public function cache_row($sql, $reload = false) { $this->reload = $reload; $sql = $this->get_query_sql($sql, 1); return $this->get_cache($sql, 'get_row'); } //返回所有的結果集 public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) { $this->query($sql, $limit); $all_rows = array(); $this->fetch_mode = $fetch_mode; while($rows = $this->fetch()) { $all_rows[] = $rows; } $this->free(); return $all_rows; } //快取all public function cache_all($sql, $reload = false, $limit = null) { $this->reload = $reload; $sql = $this->get_query_sql($sql, $limit); return $this->get_cache($sql, 'get_all'); } //返回前一次mysql操作所影響的記錄行數 public function affected_rows() { return $this->mysqli->affected_rows; } /** * 獲取插入語句 * * @param string $tbl_name 表名 * @param array $info 資料 */ public function get_insert_db_sql($tbl_name,$info) { //首先判斷是否為陣列,再判斷陣列是否為空 if(is_array($info)&&!empty($info)) { $i = 0; foreach($info as $key=>$val) { $fields[$i] = $key; //將所有的鍵名放到一個$fields[]陣列中 $values[$i] = $val; //將所有的值放到一個$values[]陣列中 $i++; } $s_fields = "(".implode(",",$fields).")"; $s_values = "('".implode("','",$values)."')"; $sql = "INSERT INTO $tbl_name $s_fields VALUES $s_values"; Return $sql; } else { Return false; } } /** * 獲取替換語句:replace into是insert into的增強版 * 區別:replace into跟insert功能類似,不同點在於:replace into 首先嚐試插入資料到表中,如果發現表中 已經有此行資料(根據主鍵或唯一索引判斷),則先刪除此行資料,然後插入新的資料,否則直接插入新資料 * @param string $tbl_name 表名 * @param array $info 資料 */ public function get_replace_db_sql($tbl_name,$info) { if(is_array($info)&&!empty($info)) { $i = 0; foreach($info as $key=>$val) { $fields[$i] = $key; $values[$i] = $val; $i++; } $s_fields = "(".implode(",",$fields).")"; $s_values = "('".implode("','",$values)."')"; $sql = "REPLACE INTO $tbl_name $s_fields VALUES $s_values"; Return $sql; } else { Return false; } } /** * 獲取更新SQL語句 * * @param string $tbl_name 表名 * @param array $info 資料 * @param array $condition 條件 */ public function get_update_db_sql($tbl_name,$info,$condition) { $i = 0; $data = ''; if(is_array($info)&&!empty($info)) { foreach( $info as $key=>$val ) { if(isset($val)) { $val = $val; if($i==0&&$val!==null) { $data = $key."='".$val."'"; //第一次:如,update 表名 set username='admin' } else { $data .= ",".$key." = '".$val."'";//非第一次:如, ,password='123' } $i++; } } $sql = "UPDATE ".$tbl_name." SET ".$data." WHERE ".$condition; return $sql; } else { Return false; } } /** * 取得資料庫最後一個插入ID * * @return int */ public function last_id() { return mysqli_insert_id($this->mysqli); } public function real_get($sql, $fetch_mode = MYSQLI_ASSOC) { $this->query($sql); $this->fetch_mode = $fetch_mode; $row = $this->fetch(); $this->free(); return $row; } }
在model中使用:
$tbl_product = TBL_PRE . "products"; $tbl_goods = TBL_PRE . "goods"; $tbl_brand = TBL_PRE . "goods_brand"; $tbl_category = TBL_PRE . "goods_category"; $tbl_type = TBL_PRE . "goods_type"; $tbl_spec = TBL_PRE ."goods_specification"; $sql = " SELECT a.*, b.brand_name, b.brand_name_second,b.brand_logo,b.brand_simple_intro, b.url_path,c.category_name,c.url_path AS cate_url_path, sp.spec_name, sp.spec_memo FROM (SELECT g.*,p.product_id, p.bn, p.bar_code, p.color_id, p.color_text,p.small_list_page_product_pic_default, p.small_list_page_product_pic_replace,p.is_new, p.is_promotion,p.is_newstyle,p.is_limit,p.is_classic,p.is_new_spring,p.act_id, p.product_name, p.price as product_price, p.market_price as product_market_price, p.intro, p.editor_intro,p.size_image_path, p.size_intro, p.video_path, p.size_image_name, p.status as pstatus, p.is_stop as pisstop FROM $tbl_product p LEFT JOIN $tbl_goods g ON p.goods_id = g.goods_id WHERE p.product_id = $id $_filter) a LEFT JOIN $tbl_brand b ON a.brand_id = b.brand_id LEFT JOIN $tbl_category c ON a.category_id = c.category_id LEFT JOIN $tbl_spec sp ON a.spec_id = sp.spec_id"; $result['base'] = $this->db->cache_row($sql);