CI框架 數據庫操作

1、查詢表記錄

$this->db->select();       //選擇查詢的字段
$this->db->select_max();
$this->db->select_min();
$this->db->select_avg();
$this->db->select_sum();
$this->db->from();         //選擇表名
$this->db->join();
$this->db->get();          //得到查詢結果
$this->db->get_where();
$this->db->where();
$this->db->or_where();
$this->db->where_in();
$this->db->or_where_in();
$this->db->where_not_in();
$this->db->or_where_not_in();
$this->db->like();
$this->db->or_like();
$this->db->not_like();
$this->db->or_not_like();
$this->db->group_by();
$this->db->distinct();
$this->db->having();
$this->db->or_having();
$this->db->order_by();
$this->db->limit();
$this->db->count_all_results();


 

2、增加表記錄

$this->db->insert();


 

3、更改表記錄

$this->db->set();
$this->db->update();

4、刪除表記錄

$this->db->delete();

 

5、清空表記錄

$this->db->empty_table();
$this->db->truncate();


 

6、緩存部分

$this->db->start_cache()
$this->db->stop_cache()
$this->db->flush_cache()


 

7、結果結果集

result()        //返回對象數組
result_array()  //返回二維數組
row()           //返回一個對象
row_array()     //返回一維數組
num_rows()      //返回查詢的行數
num_fields()    //返回查詢結果的字段數
free_result()   //釋放查詢所佔的資源內存


 

8、輔助查詢函數

$this->db->insert_id()       //獲取剛剛插入的id
$this->db->affected_rows()   //修改或插入影響的行數
$this->db->count_all();      //統計記錄的總條數 這一函數加入where條件無效
$this->db->last_query();     //最後一條執行的sql語句

//以下兩個函數僅返回sql語句 不執行sql語句

$data = array('name' => $name, 'email' => $email, 'url' => $url);
$str = $this->db->insert_string('table_name', $data);

$data = array('name' => $name, 'email' => $email, 'url' => $url);
$where = "author_id = 1 AND status = 'active'";
$str = $this->db->update_string('table_name', $data, $where); //返回正確格式的更新字符串

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章