Codeigniter query builder using implode function i

2019-03-02 07:10发布

问题:

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 :)

回答1:

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


回答2:

You can just write this :

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

Official doc

$names = array('Frank', 'Todd', 'James');

$this->db->where_in('username', $names);

// Produces: WHERE username IN ('Frank', 'Todd', 'James')