Codeigniter has been used for some time and has not been summarized. Now I will summarize some optimization methods for Codeigniter to operate database tables. For more information, see the CI framework.
Codeigniter has been used for some time and has not been summarized. 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 form
$ 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