- /**
- * Class DB
- * 資料庫操作類
- */
- class DB {
- /**
- * @var
- * @return CDB
- */
- private static $db;
- /**擷取CDb類
- * @param $table_name 表名稱
- * @param string $db_setting 調用資料庫配置項
- * @param array $db_config 資料庫配置
- * @return CDb
- */
- public static function cdb($table_name='',$db_setting='default',$db_config=array()){
- if(!isset(self::$db)){
- $db = new CDb($table_name,$db_setting,$db_config);
- self::$db=$db;
- }else{
- $db=self::$db;
- }
- return $db;
- }
- /** 配置
- * @param $table_name 表名稱
- * @param string $db_setting 調用資料庫配置項
- * @param array $db_config 資料庫配置
- * @return CDb
- */
- public static function init($table_name='',$db_setting='default',$db_config=array()) {
- return self::cdb($table_name,$db_setting,$db_config);
- }
- /**
- * 執行刪除記錄操作
- * @param $table 表名稱
- * @param $condition 刪除資料條件,不充許為空白。可以為數組
- * @return boolean
- */
- public static function delete($table, $condition) {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->delete($condition);
- }
- /**
- * 執行添加記錄操作
- * @param $table 表名稱
- * @param array $data要增加的資料,參數為數組。數組key為欄位值,數組值為資料取值
- * @param bool $return_insert_id 是否返回建立ID號
- * @param bool $replace 是否採用 replace into的方式添加資料
- * @return boolean
- */
- public static function insert($table, $data, $return_insert_id = false, $replace = false) {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->insert($data, $return_insert_id, $replace);
- }
- /**
- * 擷取最後一次添加記錄的主鍵號
- * @return int
- */
- public static function insertID() {
- $db=self::cdb();
- return $db->insert_id();
- }
- /**
- * 執行更新記錄操作
- * @param $table 表名稱
- * @param $data 要更新的資料內容,參數為數組
- * 為數組時數組key為欄位值,數組值為資料取值
- * 為數組時[例: array('name'=>'lanmps','password'=>'123456')]
- * 數組的另一種使用array('name'=>'+=1', 'base'=>'-=1');程式會自動解析為`name` = `name` + 1, `base` = `base` - 1
- * 字串,請按照格式 :
- * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * @param $where 更新資料時的條件,
- * 字串,請按照格式 :
- * 字串 [例1:" id=1 and time>$time " ]
- * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
- * @return boolean
- */ bbs.it-home.org
- public static function update($table, $data, $where) {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->update($data,$where);
- }
- /**
- * 擷取單條記錄查詢
- * @param array $SQL 查詢條件陳述式
- * @return array/null資料查詢結果集,如果不存在,則返回空
- */
- public static function fetchFirst($sql) {
- $db=self::cdb();
- return $db->fetch($sql);
- }
- /**
- * 執行sql查詢
- * @param $sql查詢條件
- * @return array 查詢結果集數組
- */
- public static function fetchAll($sql) {
- $db=self::cdb();
- return $db->fetchAll($sql);
- }
- /**
- * 直接執行sql查詢
- * @param $sql 查詢sql語句
- * @return
- */
- public static function query($sql) {
- $db=self::cdb();
- return $db->exec($sql);
- }
- /**
- * 執行sql查詢
- * @param $table 表名稱
- * @param $where 查詢條件
- * 字串,請按照格式 :
- * 字串 [例1:" id=1 and time>$time " ]
- * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
- * @param $fields需要查詢的欄位值[例`name`,`gender`,`birthday`]
- * @param $limit 返回結果範圍[例:10或10,10 預設為空白]
- * @param $order 排序方式 [預設按資料庫預設排序]
- * @param $group 分組方式 [預設為空白]
- * @return array 查詢結果集數組
- */
- public static function select($table,$where = '', $fields = '*', $limit = '', $order = '', $group = '') {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->select($where , $fields , $limit, $order , $group);
- }
- /**
- * 擷取單條記錄查詢
- * @param $table 表名稱
- * @param array $where查詢條件陳述式
- * 字串,請按照格式 :
- * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
- * @param string $fields 需要查詢的欄位值[例`name`,`gender`,`birthday`]
- * @param string $order 排序方式 [預設按資料庫預設排序]
- * @param string $group 分組方式 [預設為空白]
- * @return array/null資料查詢結果集,如果不存在,則返回空
- */
- public static function getOne($table,$where,$fields = '*', $order = '', $group = '') {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->get_one($where , $fields,$order, $group);
- }
- /**
- * 查詢多條資料並分頁
- * @param $table 表名稱
- * @param $where 查詢條件
- * 字串,請按照格式 :
- * 字串 [例1:" id=1 and time>$time " ]
- * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
- * @param $fields欄位 *,id
- * @param $order 排序 id desc ,orderlist asc
- * @param $page 頁碼 1
- * @param $pagesize 每頁條數
- * @return array('data'=>資料,'count'=>記錄總數)
- */
- public static function listInfo($table,$where = '',$fields='*', $order = '', $page = 1, $pagesize = 20) {
- $db=self::cdb();
- $db->setTableName($table);
- $d=$db->listinfo($where,$fields, $order, $page, $pagesize);
- return array('data'=>$d,'count'=>self::$db->number);
- }
- /**第一個參數值
- * @param $sql
- * @return mixed
- */
- public static function resultFirst($sql){
- $db=self::cdb();
- return $db->resultFirst($sql);
- }
- }
複製代碼調用方法: DB::insert('test',array('name'=>'test')); |