该方法提供了4种方式让你编写查询语句中的 WHERE 子句: 注解( 全部的数据将会自动转义,生成安全的查询语句。 )数组
1. 简单的 key/value 方式:安全
$this->db->where('name', $name); // Produces: WHERE name = 'Joe'
注意自动为你加上了等号。this
若是你屡次调用该方法,那么多个 WHERE 条件将会使用 AND 链接起来:spa
$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 方式: 字符串
为了控制比较,你能够在第一个参数中包含一个比较运算符:it
$this->db->where('name !=', $name); $this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45
3. 关联数组方式:date
$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);
$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);