Optimization of Codeigniter operations on database tables _ PHP Tutorial

Source: Internet
Author: User
A summary of the optimization statements for Codeigniter database tables. Codeigniter has been used for some time and has not been summarized. Now I have summarized some optimization statements for Codeigniter to operate on database tables. although they are incomplete, they can also help those who have just used codeigniter for a while and have not made any summary. Now I will summarize some optimization methods for Codeigniter to operate database tables. although not all, it can help those who have just started CI.

Link database

The code is as follows:

$ This-> load-> database (); // manually connect to the database
// Connect to multiple databases
$ DB1 = $ this-> load-> database ('group _ one', TRUE );
$ DB2 = $ this-> load-> database ('group _ two', TRUE );

Query

The code is as follows:

// Parameter binding format
$ SQL = "SELECT * FROM some_table WHERE id =? AND status =? AND author =? ";
$ This-> db-> query ($ SQL, array (3, 'live', 'Rick '));

// Multi-result standard query
$ Query = $ this-> db-> query ($ SQL); // custom
$ Query = $ this-> db-> get ('tablename'); // convenient form, equivalent to: SELECT * FROM tablename
$ Query = $ this-> db-> get ('tablename', 10, 20); // equivalent to: SELECT * FROM tablename LIMIT 20, 10

$ Query-> result () // object format
$ Query-> result_array () // array format
/*
Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
Echo $ row-> name;
Echo $ row-> email;
}
*/
$ Query-> num_rows () // The total number of rows
$ Query-> num_fields () // number of fields

// Standard query of a single result
$ Row = $ query-> row (); // object format
$ Row = $ query-> row_array (); // array format
/*
$ Row = $ query-> row_array ();
Echo $ row ['name'];
*/


Insert

The code is as follows:

$ Data = array (
'Title' => $ title,
'Name' => $ name
);
$ This-> db-> insert ('tablename', $ data); // convenient insert
$ This-> db-> insert_string ('tablename', $ data); // easy to insert

$ This-> db-> insert_id () // the newly inserted id
$ This-> db-> affected_rows () // The number of affected rows (update, insert)

Update

The code is as follows:

$ Data = array (
'Name' => $ name,
'Email '=> $ email
);
$ Where = "id = 1 ";
$ This-> db-> update ('tablename', $ data );
$ This-> db-> update_string ('tablename', $ data, $ where );

Delete

The code is as follows:

$ Array = array (
'Name' => $ name,
'Title' => $ title
);
$ This-> db-> delete ('tablename', $ array );

// Produces:
// "Delete from tablename WHERE name = '$ name' AND title =" $ title ""

$ This-> db-> truncate ('tablename'); // clear the table
// Produce: TRUNCATE tablename



-----------------------------------------------------
(Where)
-------

$ Array = array (
'Name' => $ name,
'Title' => $ title
);
$ This-> db-> where ($ array );
// Produces: "WHERE name = '$ name' AND title =" $ title ""
-----------------------------------------------------
$ This-> db-> count_all ('tablename'); // The total number of records in the table
-----------------------------------------------------
$ Query-> free_result () // release resources

Bytes. Now I want to summarize some optimization methods for Codeigniter to operate database tables. although not all, it can help those who just...

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.