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...