Codeigniter database operation function Summary

Source: Internet
Author: User

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 ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.