CI 搜索 $this->db->where()

好久不见. 提交于 2020-02-27 08:42:49

该方法提供了4种方式让你编写查询语句中的 WHERE 子句: 注解( 所有的数据将会自动转义,生成安全的查询语句。

1. 简单的 key/value 方式:

$this->db->where('name', $name); // Produces: WHERE name = 'Joe'

 

注意自动为你加上了等号。

如果你多次调用该方法,那么多个 WHERE 条件将会使用 AND 连接起来:

$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);
// WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

2. 自定义 key/value 方式:  

为了控制比较,你可以在第一个参数中包含一个比较运算符:

$this->db->where('name !=', $name);
$this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45

3. 关联数组方式:

$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

你也可以在这个方法里包含你自己的比较运算符:

$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);
4. 自定义字符串: 你可以完全手工编写  WHERE  子句:
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

$this->db->where() 方法有一个可选的第三个参数,如果设置为 FALSE,CodeIgniter 将不保护你的表名和字段名。

$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!