There are many Codeigniter database operations on the Internet. Here is 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 () returns data
$ Data = $ query-> result_array ();
// Row () returns only one array of Objects
$ Data = $ query-> row ();
// Number of rows returned by num_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 array row
$ Data = $ query-> row_array ();
// Free_result () releases the memory occupied by the current query and deletes the associated resource ID
$ Data = $ query-> free_result ();
/*
========================================
Insert operation
========================================
*/
// ID generated during 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 ();
// Return the last running query statement
Echo $ this-> db-> last_query ();
// Insert 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 operation
========================================
*/
// Update data. The updated data is 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
========================================
*/
// Obtain all the table data
$ This-> db-> get ('table _ name ');
// The second parameter is the number of output records, and the third parameter is the start position.
$ This-> db-> get ('table _ name', 10, 20 );
// Obtain data. The first parameter is the table name, the second parameter is the query condition, and the third parameter is the number of items.
$ This-> db-> get_where ('table _ name', array ('id' => $ id), $ offset );
// Obtain data in select mode
$ This-> db-> select ('title, content, date ');
$ Data = $ this-> db-> get ('table _ name ');
// Obtain the maximum value of a field. The second parameter is an alias, which is equivalent to max (age) AS nianling.
$ This-> db-> select_max ('age ');
$ This-> db-> select_max ('age', 'nianling ');
// Obtain the minimum value of a field
$ This-> db-> select_min ('age ');
$ This-> db-> select_min ('age', 'nianling ');
// Obtain the sum of fields
$ This-> db-> select_sum ('age ');
$ This-> db-> select_sum ('age', 'nianling ');
// Custom from table
$ This-> db-> select ('title', content, date ');
$ This-> db-> from ('table _ name ');
// Query condition WHERE name = 'job' AND title = "boss" AND status = 'active'
$ This-> db-> where ('name', $ name );
$ This-> db-> where ('title', $ title );
$ This-> db-> where ('status', $ status );
// Query the range
$ This-> db-> where_in ('item1', 'item2 ');
$ This-> db-> where_not_in ('item1', 'item2 ');
// Match. The third parameter is the matching mode title LIKE '% match %'
$ This-> db-> like ('title', 'match', 'before/after/both ');