codeigniter數(shù)據(jù)庫操作函數(shù)匯總
更新時(shí)間:2014年06月12日 14:46:47 投稿:shichen2014
網(wǎng)上倒是有不少Codeigniter數(shù)據(jù)庫操作的介紹,這里做一個(gè)匯總,需要的朋友可以參考下
網(wǎng)上倒是有不少Codeigniter數(shù)據(jù)庫操作的介紹,這里做一個(gè)匯總。
復(fù)制代碼 代碼如下:
//查詢:
$query = $this->db_query("SELECT * FROM table");
==================================
//result() 返回對(duì)象數(shù)組
$data = $query->result();
//result_array() 返回?cái)?shù)據(jù)
$data = $query->result_array();
//row() 只返回一行對(duì)象數(shù)組
$data = $query->row();
//num_rows() 返回查詢結(jié)果行數(shù)
$data = $query->num_rows();
//num_fields() 返回查詢請(qǐng)求的字段個(gè)數(shù)
$data = $query->num_fields();
//row_array() 只返回一行數(shù)組
$data = $query->row_array();
//free_result() 釋放當(dāng)前查詢所占用的內(nèi)存并刪除關(guān)聯(lián)資源標(biāo)識(shí)
$data = $query->free_result();
/*
==================================
插入操作
==================================
*/
//上次插入操作生成的ID
echo $this->db->insert_id();
//寫入和更新操作被影響的行數(shù)
echo $this->db->affected_rows();
//返回指定表的總行數(shù)
echo $this->db->count_all('table_name');
//輸出當(dāng)前的數(shù)據(jù)庫版本號(hào)
echo $this->db->version();
//輸出當(dāng)前的數(shù)據(jù)庫平臺(tái)
echo $this->db->platform();
//返回最后運(yùn)行的查詢語句
echo $this->db->last_query();
//插入數(shù)據(jù),被插入的數(shù)據(jù)會(huì)被自動(dòng)轉(zhuǎn)換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
$this->db->insert_string('table_name', $data);
/*
==================================
更新操作
==================================
*/
//更新數(shù)據(jù),被更新的數(shù)據(jù)會(huì)被自動(dòng)轉(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');
//第二個(gè)參數(shù)為輸出條數(shù),第三個(gè)參數(shù)為開始位置
$this->db->get('table_name', 10, 20);
//獲取數(shù)據(jù),第一個(gè)參數(shù)為表名,第二個(gè)為獲取條件,第三個(gè)為條數(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');
//獲取字段的最大值,第二個(gè)參數(shù)為別名,相當(dāng)于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');
//匹配,第三個(gè)參數(shù)為匹配模式 title LIKE '%match%'
$this->db->like('title', 'match', 'before/after/both');
$query = $this->db_query("SELECT * FROM table");
==================================
//result() 返回對(duì)象數(shù)組
$data = $query->result();
//result_array() 返回?cái)?shù)據(jù)
$data = $query->result_array();
//row() 只返回一行對(duì)象數(shù)組
$data = $query->row();
//num_rows() 返回查詢結(jié)果行數(shù)
$data = $query->num_rows();
//num_fields() 返回查詢請(qǐng)求的字段個(gè)數(shù)
$data = $query->num_fields();
//row_array() 只返回一行數(shù)組
$data = $query->row_array();
//free_result() 釋放當(dāng)前查詢所占用的內(nèi)存并刪除關(guān)聯(lián)資源標(biāo)識(shí)
$data = $query->free_result();
/*
==================================
插入操作
==================================
*/
//上次插入操作生成的ID
echo $this->db->insert_id();
//寫入和更新操作被影響的行數(shù)
echo $this->db->affected_rows();
//返回指定表的總行數(shù)
echo $this->db->count_all('table_name');
//輸出當(dāng)前的數(shù)據(jù)庫版本號(hào)
echo $this->db->version();
//輸出當(dāng)前的數(shù)據(jù)庫平臺(tái)
echo $this->db->platform();
//返回最后運(yùn)行的查詢語句
echo $this->db->last_query();
//插入數(shù)據(jù),被插入的數(shù)據(jù)會(huì)被自動(dòng)轉(zhuǎn)換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
$this->db->insert_string('table_name', $data);
/*
==================================
更新操作
==================================
*/
//更新數(shù)據(jù),被更新的數(shù)據(jù)會(huì)被自動(dòng)轉(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');
//第二個(gè)參數(shù)為輸出條數(shù),第三個(gè)參數(shù)為開始位置
$this->db->get('table_name', 10, 20);
//獲取數(shù)據(jù),第一個(gè)參數(shù)為表名,第二個(gè)為獲取條件,第三個(gè)為條數(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');
//獲取字段的最大值,第二個(gè)參數(shù)為別名,相當(dāng)于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');
//匹配,第三個(gè)參數(shù)為匹配模式 title LIKE '%match%'
$this->db->like('title', 'match', 'before/after/both');
您可能感興趣的文章:
- codeigniter自帶數(shù)據(jù)庫類使用方法說明
- 讓CodeIgniter數(shù)據(jù)庫緩存自動(dòng)過期的處理的方法
- 新浪SAE云平臺(tái)下使用codeigniter的數(shù)據(jù)庫配置
- Codeigniter操作數(shù)據(jù)庫表的優(yōu)化寫法總結(jié)
- CodeIgniter針對(duì)數(shù)據(jù)庫的連接、配置及使用方法
- CodeIgniter框架數(shù)據(jù)庫事務(wù)處理的設(shè)計(jì)缺陷和解決方案
- CI框架(CodeIgniter)實(shí)現(xiàn)的數(shù)據(jù)庫增刪改查操作總結(jié)
- CodeIgniter框架數(shù)據(jù)庫基本操作示例
- CI(CodeIgniter)框架配置
- CodeIgniter基本配置詳細(xì)介紹
- php框架CodeIgniter主從數(shù)據(jù)庫配置方法分析
相關(guān)文章
thinkPHP5.0框架整體架構(gòu)總覽【應(yīng)用,模塊,MVC,驅(qū)動(dòng),行為,命名空間等】
這篇文章主要介紹了thinkPHP5.0框架整體架構(gòu),簡(jiǎn)單介紹了thinkPHP5.0的應(yīng)用,模塊,MVC,驅(qū)動(dòng),行為,命名空間等概念與基本用法,需要的朋友可以參考下2017-03-03php截取字符串之截取utf8或gbk編碼的中英文字符串示例
php中自帶strlen是返回的字節(jié)數(shù),對(duì)于utf8編碼的中文返回時(shí)3個(gè),不滿足需求,下面給大家提供一個(gè)方法來完成這樣的功能2014-03-03PHP laravel中的多對(duì)多關(guān)系實(shí)例詳解
數(shù)據(jù)表之間是縱橫交叉、相互關(guān)聯(lián)的,laravel的一對(duì)一,一對(duì)多比較好理解,本文重點(diǎn)通過實(shí)例給大家講解 laravel中的多對(duì)多關(guān)系,感興趣的朋友一起看看吧2017-06-06創(chuàng)建配置文件 用PHP寫出自己的BLOG系統(tǒng) 2
今天做博客安裝程序,首先做的是配置文件的創(chuàng)建。2010-04-04smarty模板引擎之配置文件數(shù)據(jù)和保留數(shù)據(jù)
這篇文章主要介紹了smarty模板引擎之配置文件數(shù)據(jù)和保留數(shù)據(jù)的方法,實(shí)例分析了smarty模板引擎配置文件數(shù)據(jù)及獲取數(shù)據(jù)的具體技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03ThinkPHP5 框架引入 Go AOP,PHP AOP編程項(xiàng)目詳解
這篇文章主要介紹了ThinkPHP5 框架引入 Go AOP,PHP AOP編程,結(jié)合具體項(xiàng)目項(xiàng)目分析了ThinkPHP5 引入 Go AOP,PHP AOP編程相關(guān)概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05