2017-02-22 144 views
0

我正在嘗試使用CI執行通配符搜索查詢。CodeIgniter通配符查詢

我正在檢查登錄會話。如果用戶登錄到多個設備,則會顯示一個彈出窗口。要檢查用戶是否從任何其他設備登錄,我執行通配符查詢以檢查其user_data列中是否存在其用戶標識並返回行數。我面臨的問題是,即使我第一次登錄,它會轉到else子句,而不是if或if-else子句。

在我的控制器中,我檢查它是否到達else子句,顯示一個彈出窗口。但是,即使有人第一次登錄,它也會進入else子句。

型號:

public function update_bool($id) { 
     $this->db->select('user_data'); 
     $this->db->from('ci_sess'); 
     $this->db->like('user_data', $id, 'both'); 
     $counter = $this->db->get()->row(); 
      if (empty($counter)) $counter = 0; 
      else if($counter === 1) $counter = 1; 
      else $counter = 3; 
     return $counter; 
    } 

控制器:

$counter = $this->ion_auth_model->loggedin_status_update_bool($userId); 
     if($this->ion_auth_model->loggedin_status_update_bool($userId) === 3) { 
      warning('multiple_session_title', 'multiple_session_text'); 
     } 

回答

1

你需要計算行數返回由查詢。根據沒有記錄,你的狀況將起作用。目前它返回一行數組。 And $counter as array to match else if condition will always fail.

我希望這會幫助你。

.... 
$query = $this->db->get(); 
$counter = $query->num_rows(); 
if (empty($counter)) { 
    $counter = 0; 
}else if($counter === 1) { 
    $counter = 1; 
} else { 
$counter = 3; 
} 
return $counter; 
0

我覺得你的速記可能會偏一點,如果我明白你的意思,然後會有在表相同的用戶這意味着它使用你不能得到多個會話 - >行()。

嘗試這樣的:

public function update_bool($id) { 
    $this->db->select('user_data'); 
    $this->db->from('ci_sess'); 
    $this->db->like('user_data', $id, 'both'); 
    $query = $this->db->get(); 

    return $query->num_rows(); // This will give you now many times this user exists in the database, eg 0, 1, 2, 3 
} 

然後清理你的控制器,你不調用數據庫兩次:

$counter = $this->ion_auth_model->loggedin_status_update_bool($userId); 
if($counter == 3) { 
     warning('multiple_session_title', 'multiple_session_text'); 
}