Common Operations of codeigniter on databases

Source: Internet
Author: User
Tags codeigniter
Www. thenbsp. comview-codeigniter-databasecodeigniter (CI) is an excellent and agile PHP open-source framework. It is especially convenient to encapsulate database operations. The following are common database operations for phpci and a record is provided: * query $ this-db_query (SE

Http://www.thenbsp.com/view-codeigniter-database/ codeigniter (CI) is an excellent and agile PHP open source framework, especially encapsulated on the database operations, very convenient, the following is a common phpci database operations, as a record: /* ======================================== query $ query = $ this-db_query

Http://www.thenbsp.com/view-codeigniter-database/


Codeigniter (CI) is an excellent and agile PHP open-source framework, which encapsulates database operations and is very convenient. The following are common database operations of php ci, which are recorded as follows:

/* ======================================== Query $ query = $ this-> db_query ("SELECT * FROM table "); ===========================================================* // result () returned object array $ data = $ query-> result (); // result_array () returned data $ data = $ query-> result_array (); // row () returns only one array of objects $ data = $ query-> row (); // num_rows () returns the number of rows of query results $ data = $ query-> num_rows (); // num_fields () returns the number of fields in the query request $ data = $ query-> num_fields (); // row_array () returns only a row of arrays $ data = $ query-> row_array (); // free_result () release the memory occupied by the current query and delete the associated resource ID $ data = $ query-> free_result (); /* = ============================================= * // generated during the last insert operation IDecho $ this-> db-> insert_id (); // number of rows affected by write and update operations echo $ this-> db-> affected_rows (); // return the total number of rows in the specified table echo $ this-> db-> count_all ('table _ name '); // output the current database version 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, 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 ); /* = ================================================= * // obtain all tables data $ this-> db-> get ('table _ name '); // The second parameter is the number of output records, and the third parameter is the starting position $ this-> db-> get ('table _ name', 10, 20); // get data, the first parameter is the table name, the second parameter is the get condition, and the third parameter is the number of items $ this-> db-> get_where ('table _ name ', array ('id' => $ id), $ offset); // obtain data in the 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 and $ 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 ); // Range Query $ 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 '); $ this-> db-> not_like (); // group by title, date $ this-> db-> group_by ('title', 'date'); // limit the number of items $ this-> db-> limit (0, 20 );

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.