CI(CodeIgniter)框架中的增删改查操作_php实例

WBOY
Release: 2016-06-07 17:19:23
Original
739 people have browsed it

CodeIgniter的数据函数类在 \system\database\DB_active_rec.php

复制代码 代码如下:

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

连接数据库:$this->load->database();

复制代码 代码如下:

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

写在模型的构造函数里,这样加载模型的同时就连接了数据库了,非常方便。


插入数据

复制代码 代码如下:

$this->db->insert($tableName,$data);
$tableName = 是你要操作的表名。
$data=你要插入的数据,以数组的方式插入(键名=字段名,键值=字段值,自增主键不用写)。

更新数据

复制代码 代码如下:

$this->db->where('字段名','字段值');
$this->db->update('表名',修改值的数组);

查询数据

复制代码 代码如下:

$this->db->where('字段名','字段值');
$this->db->select('字段');
$query= $this->db->get('表名');
return$query->result();

删除数据

复制代码 代码如下:

$this->db->where('字段名','字段值');
$this->db->delete('表名');

接下来就要在控制器中调用我们的模型了

复制代码 代码如下:

$this->load->model('模型名')//模型名就是指你在项目目录/models/底下建的Model(与文件名相同)
$this->模型名->方法名

为了不想在每个控制器的方法里面都调用一次。我是这样做的

复制代码 代码如下:


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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!