CI框架常用函數(AR數據庫操作的常用函數)


用戶手冊地址:
http://codeigniter.org.cn/user_guide/index.html

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); //返回正確格式的更新字符串

---------------------
作者:栁羅風塵
來源:CSDN
原文:https://blog.csdn.net/wujiangwei567/article/details/44591025
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM