条件紧随其后或条件在代码初始化器中不起作用

时间:2018-09-11 11:17:38

标签: php codeigniter

当我仅使用3个OR条件中的一个OR条件时,where子句不起作用。

 $this->db->where('(delivery_date BETWEEN "'.$from.'" AND "'.$to.'") ');
    $this->db->or_where('order_date BETWEEN "'.$odfrom.'" AND "'.$odto.'"');
     $this->db->or_where('mumbai_date BETWEEN "'.$mdfrom.'" AND "'.$mdto.'"');
    $this->db->where('order_location',$sess_location);
    $this->db->where('designer_id',$sess_designer_id);
    $this->db->where('is_deleted=',0);
    $query= $this->db->limit($length,$start)->get();
return $query->result();

2 个答案:

答案 0 :(得分:1)

在codeigniter中,用括号括起来的“ or_where”条件;

$this->db->where('(delivery_date BETWEEN "'.$from.'" AND "'.$to.'") ');
$this->db->where('((order_date BETWEEN "'.$odfrom.'" AND "'.$odto
                 .'") OR (mumbai_date BETWEEN "'.$mdfrom.'" AND "'.$mdto.'"))',NULL,false);

$this->db->where('order_location',$sess_location);
$this->db->where('designer_id',$sess_designer_id);
$this->db->where('is_deleted=',0);
$query= $this->db->limit($length,$start)->get();

答案 1 :(得分:1)

尽管这个问题已经有了答案,我可能会添加-

  

您永远不要在查询中使用未转义的数据,因为您完全可以使用SQL注入-请仔细阅读QueryBuilder documentationQueries Documentation-尤其是有关转义。

可以说以下代码更适合您的情况

$query = $this->db
    ->group_start()
        ->where('delivery_date >=', $from)
        ->where('delivery_date <=', $to)
    ->group_end()   
    ->group_start()
        ->group_start()
            ->where('order_date >=', $odfrom)
            ->where('order_date <=', $odto)
        ->group_end()
        ->or_group_start()
            ->where('mumbai_date >=', $mdfrom)
            ->where('mumbai_date <=', $mdto)
        ->group_end()
    ->group_end()
    ->where('order_location', $sess_location)
    ->where('designer_id',$sess_designer_id)
    ->where('is_deleted=',0)
    ->limit($length, $start)
    ->get();

或者,如果您真的想使用between,可以这样做

$query = $this->db
    ->group_start()
        ->where('delivery_date BETWEEN '.$this->db->escape($from).' AND '.$this->db->escape($to))
    ->group_end()   
    ->group_start()
        ->group_start()
            ->where('order_date BETWEEN '.$this->db->escape($odfrom).' AND '.$this->db->escape($odto))
        ->group_end()
        ->or_group_start()
            ->where('mumbai_date BETWEEN '.$this->db->escape($mdfrom).' AND '.$this->db->escape($mdto))
        ->group_end()
    ->group_end()
    ->where('order_location', $sess_location)
    ->where('designer_id',$sess_designer_id)
    ->where('is_deleted=',0)
    ->limit($length, $start)
    ->get();