phpmysqlcodeigniteractiverecordwhere-in

CodeIgniter active record SELECT query with comma-separated value passed to where_in() only returns data for the first id in the string


I would like to get industry of a candidate from the database for that I have written the following:

$candidateID = '1,2,3,4,5,6,7,8,9';

function get_industry($candidateID) {
    $this->db->select('current_industry');
    $this->db->group_by('current_industry');
    $this->db->from('candidate_details');
    
    $this->db->where_in('user_id', $candidateID);
    
    $query = $this->db->get();
    print_r($this->db->last_query());
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}

but when I execute this query, it only returns me the industry of the first candidate out of 9.

I don't know what I am missing here.


Solution

  • Try this one

     $candidateID = '1,2,3,4,5,6,7,8,9';
    
    function get_industry($candidateID) {
      $where = 'user_id in ('.rtrim($candidateID, ",").')';
      $this->db->select('current_industry');
      $this->db->group_by('current_industry');
      $this->db->from('candidate_details');
    
      $this->db->where($where);
    
    $query = $this->db->get();
    print_r($this->db->last_query());
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
    }