最近在用codeigniter 備註下數據庫操作方法

PHP 開源中的SuperLang;隨着越來越多的 api 開放平臺和接口都在使用PHP ; 我也對PHP 進行了使用;真的很不錯;

//查詢: 

$query = $this->db_query("SELECT * FROM table");
 ==================================


//result() 返回對象數組
$data = $query->result();


//result_array() 返回數據
$data = $query->result_array();


//row() 只返回一行對象數組
$data = $query->row();


//num_rows() 返回查詢結果行數
$data = $query->num_rows();


//num_fields() 返回查詢請求的字段個數
$data = $query->num_fields();


//row_array() 只返回一行數組
$data = $query->row_array();


//free_result() 釋放當前查詢所佔用的內存並刪除關聯資源標識
$data = $query->free_result();


/*
 ==================================
 插入操作
 ==================================
*/


//上次插入操作生成的ID
echo $this->db->insert_id();


//寫入和更新操作被影響的行數
echo $this->db->affected_rows();


//返回指定表的總行數
echo $this->db->count_all('table_name');


//輸出當前的數據庫版本號
echo $this->db->version();


//輸出當前的數據庫平臺
echo $this->db->platform();


//返回最後運行的查詢語句
echo $this->db->last_query();


//插入數據,被插入的數據會被自動轉換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
$this->db->insert_string('table_name', $data);


/*
 ==================================
 更新操作
 ==================================
*/


//更新數據,被更新的數據會被自動轉換和過濾,例如:
//$data = array('name' => $name, 'email' => $email, 'url' => $url);
//$where = "author_id = 1 AND status = 'active'";
$this->db->update_string('table_name', $data, $where);


/*
 ==================================
 選擇數據
 ==================================
*/


//獲取表的全部數據
$this->db->get('table_name');


//第二個參數爲輸出條數,第三個參數爲開始位置
$this->db->get('table_name', 10, 20);


//獲取數據,第一個參數爲表名,第二個爲獲取條件,第三個爲條數
$this->db->get_where('table_name', array('id'=>$id), $offset);


//select方式獲取數據
$this->db->select('title, content, date');
$data = $this->db->get('table_name');


//獲取字段的最大值,第二個參數爲別名,相當於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');


//匹配,第三個參數爲匹配模式 title LIKE '%match%'
$this->db->like('title', 'match', 'before/after/both');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章