CodeIgniter framework common usage work summary, codeigniter framework
This example describes common usage of the CodeIgniter framework. We will share this with you for your reference. The details are as follows:
1. codeigniter controller super objects and attributes
$this->load;$this->load->database();$this->load->view();$this->load->helper();
$this->uri;$this->uri->segment(3);
$this->input;
2. Database Configuration
$this->load->database();$this->db->query('SELECT * FROM blog_user');
Configure the prefix of the exchange table
$db['default']['dbprefix'] = 'blog_';$db['default']['swap_pre'] = 'my_';
Therefore, we use the table prefix my _ when writing SQL statements, and ci will automatically change my _ to blog _. Therefore, dbprefix can be changed as needed, so that we can modify the database name.
For example:
$sql = "SELECT * FROM my_archive";
3. Form submission path
$this->load->helper('url');
Use
Site_url ('controller/method name ')
4. Form Verification(Refer to the previous article "CodeIgniter Form Verification Method Instance details" and "CI framework form verification example details")
5. SQL statement Problems
① Insert
$ This-> db-> insert ('archive', $ archive); returns the bool value $ insert_id = $ this-> db-> insert_id (); $ this-> db-> insert_batch ('archive', $ data); // insert multiple
② Query
$ Query = $ this-> db-> query ($ SQL); // return Object $ query-> num_rows () or $ query-> num_rows returns the number of results to be queried if ($ query-> num_rows ()> 0) {return $ query-> result (); // $ query-> row () $ query-> result_array () $ query-> row_array ()} else {return false;} $ query-> last_query ();
③ Update
$ Bool = $ this-> db-> where ('Id> ', '000000')-> update ('archive', $ data ); $ this-> db-> affected_rows (); // The number of affected rows.
④ Delete
$ Bool = $ this-> db-> delete ('tablename', array ('id' => '123 ')); $ bool = $ this-> db-> where (array ('id' => 500)-> delete ('tablename '); $ this-> db-> affected_rows (); // affects the row