2016-02-24 19 views
1

工作這是我的職責,我已經寫SELECT查詢凡在條件未在笨

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,這些標識

full_name user_id  user_type 

hancy   127  1 
ashish   138  3 
sofia   139  1 

出認沽應該是這樣的

+0

更好,如果你可以通過'where'在'string'格式的條件。 – Yash

+0

這是什麼 '$ user_list = $ this-> search_model-> userList(a,137,$ arr_friend);' 有137和a,你能解釋一個137嗎? –

+0

137已登錄用戶 – vaibhav

回答

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); 
0

爲了避免出現問題,你應該這樣做:

$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 '!' 

看看到文檔here

0

我改變了這一行

$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%')");