codeigniter數(shù)據(jù)庫操作函數(shù)匯總
更新時間:2014年06月12日 14:46:47 投稿:shichen2014
網(wǎng)上倒是有不少Codeigniter數(shù)據(jù)庫操作的介紹,這里做一個匯總,需要的朋友可以參考下
網(wǎng)上倒是有不少Codeigniter數(shù)據(jù)庫操作的介紹,這里做一個匯總。
復(fù)制代碼 代碼如下:
//查詢:
$query = $this->db_query("SELECT * FROM table");
==================================
//result() 返回對象數(shù)組
$data = $query->result();
//result_array() 返回數(shù)據(jù)
$data = $query->result_array();
//row() 只返回一行對象數(shù)組
$data = $query->row();
//num_rows() 返回查詢結(jié)果行數(shù)
$data = $query->num_rows();
//num_fields() 返回查詢請求的字段個數(shù)
$data = $query->num_fields();
//row_array() 只返回一行數(shù)組
$data = $query->row_array();
//free_result() 釋放當前查詢所占用的內(nèi)存并刪除關(guān)聯(lián)資源標識
$data = $query->free_result();
/*
==================================
插入操作
==================================
*/
//上次插入操作生成的ID
echo $this->db->insert_id();
//寫入和更新操作被影響的行數(shù)
echo $this->db->affected_rows();
//返回指定表的總行數(shù)
echo $this->db->count_all('table_name');
//輸出當前的數(shù)據(jù)庫版本號
echo $this->db->version();
//輸出當前的數(shù)據(jù)庫平臺
echo $this->db->platform();
//返回最后運行的查詢語句
echo $this->db->last_query();
//插入數(shù)據(jù),被插入的數(shù)據(jù)會被自動轉(zhuǎn)換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
$this->db->insert_string('table_name', $data);
/*
==================================
更新操作
==================================
*/
//更新數(shù)據(jù),被更新的數(shù)據(jù)會被自動轉(zhuǎn)換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
//$where = "author_id = 1 AND status = 'active'";
$this->db->update_string('table_name', $data, $where);
/*
==================================
選擇數(shù)據(jù)
==================================
*/
//獲取表的全部數(shù)據(jù)
$this->db->get('table_name');
//第二個參數(shù)為輸出條數(shù),第三個參數(shù)為開始位置
$this->db->get('table_name', 10, 20);
//獲取數(shù)據(jù),第一個參數(shù)為表名,第二個為獲取條件,第三個為條數(shù)
$this->db->get_where('table_name', array('id'=>$id), $offset);
//select方式獲取數(shù)據(jù)
$this->db->select('title, content, date');
$data = $this->db->get('table_name');
//獲取字段的最大值,第二個參數(shù)為別名,相當于max(age) AS nianling
$this->db->select_max('age');
$this->db->select_max('age', 'nianling');
//獲取字段的最小值
$this->db->select_min('age');
$this->db->select_min('age', 'nianling');
//獲取字段的和
$this->db->select_sum('age');
$this->db->select_sum('age', 'nianling');
//自定義from表
$this->db->select('title', content, date');
$this->db->from('table_name');
//查詢條件 WHERE name = 'Joe' AND title = "boss" AND status = 'active'
$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);
//范圍查詢
$this->db->where_in('item1', 'item2');
$this->db->where_not_in('item1', 'item2');
//匹配,第三個參數(shù)為匹配模式 title LIKE '%match%'
$this->db->like('title', 'match', 'before/after/both');
$query = $this->db_query("SELECT * FROM table");
==================================
//result() 返回對象數(shù)組
$data = $query->result();
//result_array() 返回數(shù)據(jù)
$data = $query->result_array();
//row() 只返回一行對象數(shù)組
$data = $query->row();
//num_rows() 返回查詢結(jié)果行數(shù)
$data = $query->num_rows();
//num_fields() 返回查詢請求的字段個數(shù)
$data = $query->num_fields();
//row_array() 只返回一行數(shù)組
$data = $query->row_array();
//free_result() 釋放當前查詢所占用的內(nèi)存并刪除關(guān)聯(lián)資源標識
$data = $query->free_result();
/*
==================================
插入操作
==================================
*/
//上次插入操作生成的ID
echo $this->db->insert_id();
//寫入和更新操作被影響的行數(shù)
echo $this->db->affected_rows();
//返回指定表的總行數(shù)
echo $this->db->count_all('table_name');
//輸出當前的數(shù)據(jù)庫版本號
echo $this->db->version();
//輸出當前的數(shù)據(jù)庫平臺
echo $this->db->platform();
//返回最后運行的查詢語句
echo $this->db->last_query();
//插入數(shù)據(jù),被插入的數(shù)據(jù)會被自動轉(zhuǎn)換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
$this->db->insert_string('table_name', $data);
/*
==================================
更新操作
==================================
*/
//更新數(shù)據(jù),被更新的數(shù)據(jù)會被自動轉(zhuǎn)換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
//$where = "author_id = 1 AND status = 'active'";
$this->db->update_string('table_name', $data, $where);
/*
==================================
選擇數(shù)據(jù)
==================================
*/
//獲取表的全部數(shù)據(jù)
$this->db->get('table_name');
//第二個參數(shù)為輸出條數(shù),第三個參數(shù)為開始位置
$this->db->get('table_name', 10, 20);
//獲取數(shù)據(jù),第一個參數(shù)為表名,第二個為獲取條件,第三個為條數(shù)
$this->db->get_where('table_name', array('id'=>$id), $offset);
//select方式獲取數(shù)據(jù)
$this->db->select('title, content, date');
$data = $this->db->get('table_name');
//獲取字段的最大值,第二個參數(shù)為別名,相當于max(age) AS nianling
$this->db->select_max('age');
$this->db->select_max('age', 'nianling');
//獲取字段的最小值
$this->db->select_min('age');
$this->db->select_min('age', 'nianling');
//獲取字段的和
$this->db->select_sum('age');
$this->db->select_sum('age', 'nianling');
//自定義from表
$this->db->select('title', content, date');
$this->db->from('table_name');
//查詢條件 WHERE name = 'Joe' AND title = "boss" AND status = 'active'
$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);
//范圍查詢
$this->db->where_in('item1', 'item2');
$this->db->where_not_in('item1', 'item2');
//匹配,第三個參數(shù)為匹配模式 title LIKE '%match%'
$this->db->like('title', 'match', 'before/after/both');
您可能感興趣的文章:
- codeigniter自帶數(shù)據(jù)庫類使用方法說明
- 讓CodeIgniter數(shù)據(jù)庫緩存自動過期的處理的方法
- 新浪SAE云平臺下使用codeigniter的數(shù)據(jù)庫配置
- Codeigniter操作數(shù)據(jù)庫表的優(yōu)化寫法總結(jié)
- CodeIgniter針對數(shù)據(jù)庫的連接、配置及使用方法
- CodeIgniter框架數(shù)據(jù)庫事務(wù)處理的設(shè)計缺陷和解決方案
- CI框架(CodeIgniter)實現(xiàn)的數(shù)據(jù)庫增刪改查操作總結(jié)
- CodeIgniter框架數(shù)據(jù)庫基本操作示例
- CI(CodeIgniter)框架配置
- CodeIgniter基本配置詳細介紹
- php框架CodeIgniter主從數(shù)據(jù)庫配置方法分析
相關(guān)文章
深入學(xué)習(xí)微信網(wǎng)址鏈接解封的防封原理visit_type
這篇文章主要介紹了深入學(xué)習(xí)微信網(wǎng)址鏈接解封的防封原理visit_type,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
PHP設(shè)計模式之裝飾器(裝飾者)模式(Decorator)入門與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計模式之裝飾器(裝飾者)模式(Decorator),結(jié)合實例形式詳細分析了PHP裝飾者模式的概念、原理、用法及相關(guān)操作注意事項,需要的朋友可以參考下2019-12-12
Laravel統(tǒng)一封裝接口返回狀態(tài)實例講解
這篇文章主要介紹了Laravel統(tǒng)一封裝接口返回狀態(tài)實例講解,封裝接口返回狀態(tài)有利于前后端分離項目的合作開發(fā),有正好需要的同學(xué)可以研究下2021-03-03
CI(Codeigniter)的Setting增強配置類實例
這篇文章主要介紹了Codeigniter的Setting增強配置類,結(jié)合實例形式較為詳細的分析了Codeigniter增強配置類的具體實現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-01-01

