【问题标题】:Where_in in codeigniter代码点火器中的 where_in
【发布时间】:2018-11-24 11:02:24
【问题描述】:

我正在使用 Codeigniter 3 构建网站。但是现在我从数据库中获取数据遇到了问题。

这是我的模特:

public function Get($arr_condition = null)
{
     if(!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     $query = $this->db->get('cus');
     if($query->num_rows()>0)
     {
        return $query->result_array();
     }
     return false;
}

在 Controller 中,我使用数组 ($arr_condition) 来获取条件列表:

$arr_condition['Cus_team'] =  1;
$arr_condition['Cus_user != '] =  2;
$arr_condition['Cus_phone LIKE'] = 09900000;
$arr_condition['Cus_role >'] = 1;

$result = $this->M_cus_dal->Get($arr_condition);

我的代码运行良好,直到我需要 Where_in 的条件,我尝试了:

$arr_condtion['Cus_id IN']= array('1,2,3');

但是,它不起作用。

请给我一个解决这个问题的理想方案。

【问题讨论】:

    标签: php codeigniter codeigniter-3 query-builder


    【解决方案1】:

    希望对您有所帮助:

    你可以这样做:

    在控制器中使用类似这样的另一个参数传递您的 where_in 条件

    $where_in = array('1,2,3');
    $result = $this->M_cus_dal->Get($arr_condition,$where_in);
    

    在模型方法中,将 $where_in 作为第二个参数添加到 where_in 子句中,如下所示:

    public function Get($arr_condition = null,$where_in = NULL)
    {
         if(!is_null($arr_condition))
         {
            $this->db->where($arr_condition);
         }
         if(! empty($where_in))
         {
           $this->db->where_in('Cus_id',$where_in);
         }
         $query = $this->db->get('cus');
         if($query->num_rows()>0)
         {
            return $query->result_array();
         }
         return false;
    }
    

    【讨论】:

    • 谢谢@pradeep,我尝试了第一种方法,但mysql中的结果是Cus_id = 'IN (1,2,3)'。所以我会尝试第二种方式。
    • 那么第二种方法是获得预期结果的更好方法
    • 应该是这样$arr_condtion['Cus_id IN '] = '(1,2,3)';检查它是否工作但更喜欢第二种方式,请告诉我
    • 谢谢@pradeep,我尝试了第二种方法,它工作正常。现在我没有更多时间了,所以我会根据你的理想来优化我的功能。再次感谢时间!
    • 对不起,昨天我用智能手机检查了你的答案。但我不明白今天的检查直到没有检查。 :(
    【解决方案2】:

    如果你使用 where_in 那么你的 id 应该在数组中

    像这样:

    $where_in[] = 1;
    $where_in[] = 2;
    $where_in[] = 3;
    

    $where_in = array('1','2','3');
    
    
    $this->db->where_in('Cus_id', $where_in);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2012-02-09
      • 2011-06-04
      • 2016-01-28
      • 2012-06-30
      • 2012-02-21
      相关资源
      最近更新 更多