Add, delete, modify, and query operations in the CI (CodeIgniter) Framework _ php instance

Source: Internet
Author: User
Tags codeigniter
First, create a model (project directory models). Note: The model name is the same as the file name and must inherit the data core class CI_Model, And the constructor in the parent class must be reloaded. CodeIgniter's data function class is 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 ');
}
}

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.