Add, delete, modify, and query operations in the CI (CodeIgniter) framework _ PHP Tutorial

Source: Internet
Author: User
Add, delete, modify, and query operations in the CI (CodeIgniter) framework. The code for copying the CodeIgniter data function class in systemdatabaseDB_active_rec.php is as follows: spanclassModelNameextendsCI_Model {function _ construct () CodeIgniter data function class in \ system \ database \ DB_active_rec.php

The code is as follows:


Class ModelName extends CI_Model
{
Function _ construct ()
{
Parent: :__ construct ();
}
}

Connect to the database: $ this-> load-> database ();

The code is as follows:


ClassModel_name extends CI_Model
{
Function _ construct ()
{
Parent: :__ construct ();
$ This-> load-> database ();
}
}

Written in the model constructor, so that the model can be loaded and connected to the database, which is very convenient.


Insert data

The code is as follows:


$ This-> db-> insert ($ tableName, $ data );
$ TableName = indicates the name of the table to be operated.
$ Data = the data you want to insert, which is inserted as an array (key name = field name, key value = field value, auto-incrementing primary key does not need to be written ).

Update data

The code is as follows:


$ This-> db-> where ('field name', 'Field value ');
$ This-> db-> update ('Table name', an array of modified values );

Query data

The code is as follows:


$ This-> db-> where ('field name', 'Field value ');
$ This-> db-> select ('field ');
$ Query = $ this-> db-> get ('Table name ');
Return $ query-> result ();

Delete data

The code is as follows:


$ This-> db-> where ('field name', 'Field value ');
$ This-> db-> delete ('Table name ');

Next, we need to call our model in the controller.

The code is as follows:


$ This-> load-> model ('Model name') // Model name refers to the model you created under the project directory/models/(same as the file name)
$ This-> model name-> method name

To avoid calling each controller method once. I did this.

The code is as follows:



Class ControllerName extends CI_Controller
{
Function _ construct ()
{
Parent: :__ construct ();
$ This-> load-> model ('Model name ');
}
}

Coding \ system \ database \ DB_active_rec.php code: span style = "font-size: 16px;" class ModelName extends CI_Model {function _ construct ()...

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.