Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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
Php 限制或_where到codeigniter中2个where之间的特定where_Php_Mysql_Codeigniter_Where - Fatal编程技术网

Php 限制或_where到codeigniter中2个where之间的特定where

Php 限制或_where到codeigniter中2个where之间的特定where,php,mysql,codeigniter,where,Php,Mysql,Codeigniter,Where,我有以下代码: $this->db->where('status',1); $this->db->where('category',$category_id); $this->db->or_where('parent_category',$category_id); $this->db->or_where('grandParent_category',$category_id); $this->db-&

我有以下代码:

    $this->db->where('status',1);
    $this->db->where('category',$category_id);
    $this->db->or_where('parent_category',$category_id);
    $this->db->or_where('grandParent_category',$category_id);
    $this->db->or_where('parentOfGP_category',$category_id);
    $query = $this->db->get('posts');
问题是第一个where被覆盖,它也会得到那些状态为0和2的where,因为下一个or_wheres,我希望or_wheres仅用于where('category',$category_id);而不是地位


在codeigniter中是否有这样做的方法?

您可以直接传入查询,而不是使用活动记录,如下所示:

$query = $this->db->query('SELECT name, title, email FROM my_table');
此外,如果您想确定上述活动记录查询的具体形式,请使用以下方法:

echo ($this->db->last_query());

这有点复杂,因为您需要连接
以及
条件。请看这里:

因此,对于您的情况,应该是这样的:

$this->db->select('*')->from('posts')
  ->group_start()
    ->where('category', $category_id)
    ->or_group_start()
      ->where('parent_category', $category_id)
    ->group_end()
    ->or_group_start()
      ->where('grandParent_category', $category_id);
    ->group_end()
    ->or_group_start()
      ->where('parentOfGP_category', $category_id);
    ->group_end()
  ->group_end()
  ->where('status', 1)
->get();
此代码未经测试。你可能需要调整一些东西