Codeigniter query builder using implode function in where_in

旧城冷巷雨未停 提交于 2019-12-20 04:10:14

问题


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



来源:https://stackoverflow.com/questions/40487403/codeigniter-query-builder-using-implode-function-in-where-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!