您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“codeigniter數(shù)據(jù)庫的操作函數(shù)整理”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
復(fù)制代碼 代碼如下:
//查詢:
$query = $this->db_query("SELECT * FROM table");
==================================
//result() 返回對象數(shù)組
$data = $query->result();
//result_array() 返回?cái)?shù)據(jù)
$data = $query->result_array();
//row() 只返回一行對象數(shù)組
$data = $query->row();
//num_rows() 返回查詢結(jié)果行數(shù)
$data = $query->num_rows();
//num_fields() 返回查詢請求的字段個(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)識
$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ù)庫版本號
echo $this->db->version();
//輸出當(dāng)前的數(shù)據(jù)庫平臺
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ù)庫的操作函數(shù)整理”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。