在编码点火器中不起作用的情况下


where in condition not working in in codeigniter

这是我编写选择查询的函数

function userList($name,$logged_id,$friendlist)
{
    $this->db->select("concat((mu.first_name),(' '),(mu.last_name)) AS full_name,mu.user_id,user_type",FALSE);
    $this->db->from("mst_users as mu");        
    $this->db->where("user_status", "1");
    $this->db->where_in("user_id",$friendlist);
    $this->db->where('user_id !=', $logged_id,false);
    $this->db->where("email_verified", "1");        
    $this->db->where("mu.first_name LIKE '%$name%'");        
    $this->db->or_where("mu.last_name LIKE '%$name%'");            
    //$this->db->limit("3");
    $query = $this->db->get();
    return $query->result_array();  
}

这是我从控制器调用的函数

function userList(){
    $arr_friend=array(127, 139, 138);
    $user_list = $this->search_model->userList(a,137,$arr_friend);   
}

这是查询的输出。这是错误的。

full_name   user_id     user_type   
Grace          1          2
admina         126        3
hancy          127        1
vaibhavaaa     132        1
arjun          137        1
ashish         138        3
sofia          139        1
emma           140        3
vaibhav        147        3
ashish         148        1

我只想要 127、139、138 这些 Id 的记录

full_name   user_id     user_type   
hancy          127        1 
ashish         138        3
sofia          139        1

输出应该是这样的

这是因为您以错误的方式使用where_or,请替换它:

$this->db->where("mu.first_name LIKE '%$name%'");        
$this->db->or_where("mu.last_name LIKE '%$name%'");

跟:

$this->db->where("(mu.first_name LIKE '%$name%'", FALSE);        
$this->db->or_where("mu.last_name LIKE '%$name%')", NULL, FALSE);

为避免出现问题,您应该使用以下命令:

$this->db->like('mu.first_name', $name);
$this->db->or_like('mu.last_name', $name);

您还可以控制通配符 (%) 的放置位置

$this->db->like('mu.first_name', $name, 'before');    
// Produces: WHERE `mu.first_name` LIKE '%value' ESCAPE '!'
$this->db->like('mu.first_name', $name, 'after');     
// Produces: WHERE `mu.first_name` LIKE 'value%' ESCAPE '!'
 $this->db->like('mu.first_name', $name, 'both');     
 // Produces: WHERE `mu.first_name` LIKE '%value%' ESCAPE '!'

在此处查看文档

我更改了此行

 $this->db->where("mu.first_name LIKE '%$name%'");        
 $this->db->or_where("mu.last_name LIKE '%$name%'");

对此

$this->db->where("(mu.first_name LIKE '%$name%'");        
$this->db->or_where("mu.last_name LIKE '%$name%')");