Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Codeigniter活动记录';其中_或';分组_Mysql_Codeigniter_Activerecord - Fatal编程技术网

Mysql Codeigniter活动记录';其中_或';分组

Mysql Codeigniter活动记录';其中_或';分组,mysql,codeigniter,activerecord,Mysql,Codeigniter,Activerecord,是否可以分组和\u where/或\u where/或\u like。。。语句组合在一起,以免与其他和/或where语句混合 会导致 其中城市=‘我的城市’和州=‘我的州’和(名称如%name1% 或类似%name2%的名称 对$this->db->where()允许您手动编写子句。您可以这样做: $this->db->where('city', 'My City'); $this->db->where('state', 'My State'); $this->db

是否可以分组和\u where/或\u where/或\u like。。。语句组合在一起,以免与其他和/或where语句混合

会导致

其中城市=‘我的城市’和州=‘我的州’和(名称如%name1% 或类似%name2%的名称


$this->db->where()
允许您手动编写子句。您可以这样做:

$this->db->where('city', 'My City');
$this->db->where('state', 'My State');
$this->db->where('(name LIKE %name1% OR name LIKE %name2%)', null, false);
将第三个参数设置为false可防止CodeIgniter尝试向子句添加反勾号


看看这本书。标题为$this->db->where()的部分解释了设置WHERE子句可以使用的四种方法。

我知道这有点晚了,但我在研究时发现了这篇文章,并找到了比肖恩提出的更好的解决方案

$name = 'Bob';

$this->db->where('city', 'My City');
$this->db->where('state', 'My State');
$this->db->group_start();
    $this->db->like('name', $name);
    $this->db->or_like('name', $name);
$this->db->group_end();

这两个功能
group\u start()
group\u end()
已在CodeIgniter 3.0.3中实现。根据最初的问题,这里没有更好的实现

$name1 = 'Bob';
$name2 = 'John';

$this->db->where('city', 'My City');
$this->db->where('state', 'My State');
$this->db->group_start();
$this->db->like('name', $name1);
$this->db->or_like(('name', $name2);
$this->db->group_end();

此解决方案适用于Codeigniter 3+,但不适用于Codeigniter 2