codeigniter資料庫操作函數匯總_php執行個體

來源:互聯網
上載者:User

網上倒是有不少Codeigniter資料庫操作的介紹,這裡做一個匯總。

複製代碼 代碼如下:
//查詢:
$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');

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.