Search code examples
mysqlsqlcodeigniterimplodewhere-in

Codeigniter query builder using implode function in where_in


Here is my normal sql query using implode function:

SELECT * from search_result WHERE skills IN ('".implode("','",$s_id)."');  

Now I want to convert this to codeigniter form. I tried the following code but it fails

$this->db->from('search_result');
$this->db->where_in('skills','".implode("','",$s_id)."');
$query = $this->db->get();  

Here is my $s_id array:

Array ( [0] => 2D Design [1] => 3D Design [2] => 3D Modelling ) 

So anyone please help me to do this. Thanks in advance :)


Solution

  • Official Doc say's

    $names = array('Frank', 'Todd', 'James'); # Data Array 
    $this->db->where_in('username', $names); # passing array
    

    Try like below

    Method 01(recommended )

    $this->db->from('search_result');
    $this->db->where_in('skills',$s_id);
    $query = $this->db->get();
    

    Method 02

    $this->db->from('search_result');
    $this->db->where_in('skills',implode("','",$s_id));
    $query = $this->db->get();
    

    Whats wrong on this line

    $this->db->where_in('skills','".implode("','",$s_id)."');
    

    don't wrap function with ' or " quotes. Then it will get save as STRING value to DB.


    Links

    1. where_in clause in codeigniter.com