在使用數據庫之前,我們最好將數據庫進行自動連接:config/autoload.php自動加載 $autoload['libraries'] = array('database');
一些常用函數
選擇數據
插入數據
更新數據
$this->db->update_batch();
Generates an update string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
刪除數據
鏈式方法
查詢
轉義查詢
查詢輔助函數
生成查詢記錄集
$row = $query->row(4);
結果集輔助函數
$query->num_rows()
該函數將會返回當前請求的行數。
$query->num_fields()
該函數返回當前請求的字段數(列數):
$query->free_result()
該函數將會釋放當前查詢所占用的內存并刪除其關聯的資源標識。
自動連接
$autoload['libraries'] = array('database');
手動連接
如果僅僅是一部分頁面要求數據庫連接,你可以在你有需要的函數里手工添加如下代碼或者在你的類里手工添加以供該類使用。
$this->load->database();
連接多數據庫
表數據
$this->db->list_tables();
返回一個包含當前連接數據庫中所有表名稱的數組。
$this->db->table_exists();
有時,在對某個表執行操作之前,使用該函數判斷指定表是否存在很有用。返回一個布爾值。
數據庫工具類
一旦初始化完畢,你可以通過 $this->dbutil 對象來訪問成員函數:
$this->dbutil->list_databases()
$this->dbutil->database_exists();
$this->dbutil->xml_from_result($db_result)
$this->dbutil->backup()
數據庫緩存類
一旦被激活,每一次含有數據庫查詢的頁面被加載時緩存就會自動發生。
$this->db->cache_delete_all()
清除所有所有的緩存文件。
數據庫維護類
新聞熱點
疑難解答