Online There are many CodeIgniter database operation, here to do a summary.
Copy CodeThe code is as follows://query:
$query = $this->db_query ("SELECT * from table");
==================================
Result () returns an array of objects
$data = $query->result ();
Result_array () return data
$data = $query->result_array ();
Row () returns only one row of an array of objects
$data = $query->row ();
Num_rows () returns the number of query result rows
$data = $query->num_rows ();
Num_fields () returns the number of fields in the query request
$data = $query->num_fields ();
Row_array () returns only one row of arrays
$data = $query->row_array ();
Free_result () frees the memory occupied by the current query and deletes the associated resource identity
$data = $query->free_result ();
/*
==================================
Insert operation
==================================
*/
ID generated by the last insert operation
echo $this->db->insert_id ();
Number of rows affected by write and update operations
echo $this->db->affected_rows ();
Returns the total number of rows in the specified table
echo $this->db->count_all (' table_name ');
Output the current database version number
echo $this->db->version ();
Output the current database platform
echo $this->db->platform ();
Returns the last run of the query statement
echo $this->db->last_query ();
Inserting data, the inserted data is automatically converted and filtered, for example:
$data = Array (' name ' = = $name, ' email ' = + $email, ' url ' = = $url);
$this->db->insert_string (' table_name ', $data);
/*
==================================
Update action
==================================
*/
Update data, the updated data will be automatically converted and filtered, for example:
$data = Array (' name ' = = $name, ' email ' = + $email, ' url ' = = $url);
$where = "author_id = 1 and status = ' active '";
$this->db->update_string (' table_name ', $data, $where);
/*
==================================
Select data
==================================
*/
Get all the data for a table
$this->db->get (' table_name ');
The second parameter is the number of output bars, and the third parameter is the start position
$this->db->get (' table_name ', 10, 20);
Gets the data, the first argument is the table name, the second is the get condition, and the third is the number of bars
$this->db->get_where (' table_name ', array (' id ' = = $id), $offset);
Select method to get Data
$this->db->select (' title, content, Date ');
$data = $this->db->get (' table_name ');
Gets the maximum value of the field, the second argument is an alias, and is equivalent to the MAX (age) as nianling
$this->db->select_max (' age ');
$this->db->select_max (' Age ', ' nianling ');
Gets the minimum value of the field
$this->db->select_min (' age ');
$this->db->select_min (' Age ', ' nianling ');
Gets the and of the field
$this->db->select_sum (' age ');
$this->db->select_sum (' Age ', ' nianling ');
Customizing from tables
$this->db->select (' title ', Content, date ');
$this->db->from (' table_name ');
Query criteria WHERE name = ' Joe ' and title = "Boss" and status = ' active '
$this->db->where (' name ', $name);
$this->db->where (' title ', $title);
$this->db->where (' status ', $status);
Scope Query
$this->db->where_in (' item1 ', ' item2 ');
$this->db->where_not_in (' item1 ', ' item2 ');
Match, the third parameter is a matching pattern title like '%match% '
$this->db->like (' title ', ' Match ', ' Before/after/both ');