Add, delete, modify and check operations in CI (CodeIgniter) framework_PHP tutorial

WBOY
Release: 2016-07-13 10:28:40
Original
941 people have browsed it

CodeIgniter’s data function class is in systemdatabaseDB_active_rec.php

Copy code The code is as follows:

class ModelName extends CI_Model
{
function __construct()
{
parent::__construct();
}
}

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

Copy code The code is as follows:

classModel_name extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
}< ;/span>

Write it in the constructor of the model, so that the database is connected while loading the model, which is very convenient.


Insert data

Copy code The code is as follows:

$this- >db->insert($tableName,$data);
$tableName = is the name of the table you want to operate.
$data=The data you want to insert, insert it in the form of an array (key name=field name, key value=field value, no need to write auto-incrementing primary key).

Update data

Copy code The code is as follows:

$this- >db->where('field name', 'field value');
$this->db->update('table name', array of modified values);

Query data

Copy code 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

Copy code The code is as follows:

$this- >db->where('field name','field value');
$this->db->delete('table name');

The next step is to call our model in the controller

Copy code The code is as follows:

$this- >load->model('model name')//The model name refers to the file you created in project directory/models/$this->Model name->Method name

In order not to call it once in each controller method. This is what I did

Copy code The code is as follows:


class ControllerName extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('model name');
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781407.htmlTechArticleCodeIgniter’s data function class is in systemdatabaseDB_active_rec.php. Copy the code as follows: span style="font-size:16px; "class ModelName extends CI_Model { function __construct()...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template