将Array传递给模型函数查询codeigniter

时间:2017-07-06 11:58:05

标签: php mysql codeigniter

这是我的函数我想在IN()子句中放入数组值,但它不工作
    这里$ids是传递给它的数组。

    function get_school_students_data($ids){
    $query=$this->db->query("SELECT * FROM screening_answers where uid 
    IN(".implode(',',$ids).")");
    $result=$query->result_array();
     return $query->num_rows();
    }

3 个答案:

答案 0 :(得分:0)

试试这个

$this->db->where_in('uid',$ids);

所以在模型中

function get_school_students_data($ids){
    $this->db->where_in('uid',$ids);//$ids should be an array like array('20','15','22','42','86')
    $query = $this->db->get("screening_answers");
    $result=$this->db->result_array();// no need of this in your scenario
    return $this->db->num_rows();
 }

答案 1 :(得分:0)

最好使用codeigniter的活动记录

$this->db->query("SELECT * FROM screening_answers where uid 
IN(".implode(',',$ids).")");

成为

$this->db->select();
$this->db->from('screening_answer');
$this->db->where_in('uid',$ids);

答案 2 :(得分:0)

试试这项工作对我来说 //对于简单/随机搜索

 function search($designation){
        $this->db->select("*");
        foreach($designation as $single_data) $this->db->or_like("designation",$single_data);
        $this->db->order_by('rand()');
        $this->db->limit(100);
        $this->db->from('content');
        $query = $this->db->get();
        return $query->result();
    }