數據庫的其他類繼承的都是libs/class/model.class.php
這里面有寫好的操作數據庫的常用方法
1.增
insert($data, $return_insert_id = false, $replace = false)
/** * 執行添加記錄操作 * @param $data 要增加的數據,參數為數組。數組key為字段值,數組值為數據取值 * @param $return_insert_id 是否返回新建ID號 * @param $replace 是否采用 replace into的方式添加數據 * @return boolean */ insert($data, $return_insert_id = false, $replace = false)
union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='')
/** * 兩個表聯合查詢分頁 * @param string $main_table 主表 * @param string $secondary_table 副表 * @param string $data 查詢字段 * @param string $where 條件 * @param string $order 排序 * @param string $page 分頁 * @param string $pagesize 每頁記錄數 * @return array * @author:hans */ union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='')
2.刪除
delete($where)
/** * 執行刪除記錄操作 * @param $where 刪除數據條件,不充許為空。 * @return boolean */ delete($where)
3.改
update($data, $where = '')
/** * 執行更新記錄操作 * @param $data 要更新的數據內容,參數可以為數組也可以為字符串,建議數組。 * 為數組時數組key為字段值,數組值為數據取值 * 為字符串時[例:`name`='phpcms',`hits`=`hits`+1]。 * 為數組時[例: array('name'=>'phpcms','password'=>'123456')] * 數組的另一種使用array('name'=>'+=1', 'base'=>'-=1');程序會自動解析為`name` = `name` + 1, `base` = `base` - 1 * @param $where 更新數據時的條件,可為數組或字符串 * @return boolean */ update($data, $where = '')
4.查
select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='')
/** * 執行sql查詢 * @param $where 查詢條件[例`name`='$name'] * @param $data 需要查詢的字段值[例`name`,`gender`,`birthday`] * @param $limit 返回結果范圍[例:10或10,10 默認為空] * @param $order 排序方式 [默認按數據庫默認方式排序] * @param $group 分組方式 [默認為空] * @param $key 返回數組按鍵名排序 * @return array 查詢結果集數組 */ final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') { if (is_array($where)) $where = $this->sqls($where); return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key); }
get_one($where = '', $data = '*', $order = '', $group = '')
/** * 獲取單條記錄查詢 * @param $where 查詢條件 * @param $data 需要查詢的字段值[例`name`,`gender`,`birthday`] * @param $order 排序方式 [默認按數據庫默認方式排序] * @param $group 分組方式 [默認為空] * @return array/null 數據查詢結果集,如果不存在,則返回空 */ final public function get_one($where = '', $data = '*', $order = '', $group = '') { if (is_array($where)) $where = $this->sqls($where); return $this->db->get_one($data, $this->table_name, $where, $order, $group); }
query($sql) 用query來執行sql,得到的結果需要遍歷一次才是數組
/** * 直接執行sql查詢 * @param $sql 查詢sql語句 * @return boolean/query resource 如果為查詢語句,返回資源句柄,否則返回true/false */ final public function query($sql) { $sql = str_replace('phpcms_', $this->db_tablepre, $sql); return $this->db->query($sql); }
listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*')
查詢多條記錄並且分頁,這里用到的分頁和后台的分頁是相同的樣式(到后台看操作日志的分頁),如果需要修改樣式的話,需要賦值框架自己的分頁,重命名后再修改,網上有很多例子。
/** * 查詢多條數據並分頁 * @param $where * @param $order * @param $page * @param $pagesize * @return unknown_type */ final public function listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*') { $where = to_sqls($where); $this->number = $this->count($where); $page = max(intval($page), 1); $offset = $pagesize*($page-1); $this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages); $array = array(); if ($this->number > 0) { return $this->select($where, $data, "$offset, $pagesize", $order, '', $key); } else { return array(); } }
union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='')
多表鏈接查詢,類似tp里面的leftjoin,innerjoin 一般用於主表和附表鏈
public function union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='') { /*獲取記錄總數*/ $query_count = "SELECT count(*) AS `total` FROM $main_table AS m INNER JOIN $secondary_table AS s ON $union_on WHERE $where"; $this->db->query($query_count); $counts = $this->db->fetch_next(); $this->number = $counts['total']; /*設置分頁*/ $page = max(intval($page), 1); $offset = $pagesize*($page-1); $this->pages = pages($this->number, $page, $pagesize); /*獲取記錄*/ $array = array(); if ($this->number > 0) { $query = "SELECT $fields FROM $main_table AS m"; $query .= " INNER JOIN $secondary_table AS s ON $union_on"; $query .= " WHERE $where"; if($order) $query .= " ORDER BY $order";//排序 if($limit) $query .= " LIMIT $limit"; else $query .= " LIMIT $offset, $pagesize"; $this->db->query($query); $data = array(); while($r = $this->db->fetch_next()) { $data[] = $r; } return $data; } else { return array(); } }
其他查詢 目前還沒用到:
count($where = '')
/** * 計算記錄數 * @param string/array $where 查詢條件 */ final public function count($where = '') { $r = $this->get_one($where, "COUNT(*) AS num"); return $r['num']; }
insert_id()
/** * 獲取最后一次添加記錄的主鍵號 * @return int */ final public function insert_id() { return $this->db->insert_id(); }
affected_rows()
get_primary()
get_fields($table_name = '')
table_exists($table)
field_exists($field)
fetch_array()
/** * 獲取最后數據庫操作影響到的條數 * @return int */ final public function affected_rows() { return $this->db->affected_rows(); } /** * 獲取數據表主鍵 * @return array */ final public function get_primary() { return $this->db->get_primary($this->table_name); } /** * 獲取表字段 * @param string $table_name 表名 * @return array */ final public function get_fields($table_name = '') { if (empty($table_name)) { $table_name = $this->table_name; } else { $table_name = $this->db_tablepre.$table_name; } return $this->db->get_fields($table_name); } /** * 檢查表是否存在 * @param $table 表名 * @return boolean */ final public function table_exists($table){ return $this->db->table_exists($this->db_tablepre.$table); } /** * 檢查字段是否存在 * @param $field 字段名 * @return boolean */ public function field_exists($field) { $fields = $this->db->get_fields($this->table_name); return array_key_exists($field, $fields); } final public function list_tables() { return $this->db->list_tables(); } /** * 返回數據結果集 * @param $query (mysql_query返回值) * @return array */ final public function fetch_array() { $data = array(); while($r = $this->db->fetch_next()) { $data[] = $r; } return $data; }