2013-08-23 87 views
0

我是新來的笨並有下面的代碼:笨訪問結果集

$userAuthenticated = $this->Membership_model->userLogin($email, $pass); 

和模型,下面是方法:

function userLogin($email, $password) 
    { 
     $this->db->select('*'); 
     $this->db->from('members'); 
     $this->db->where('email', $email); 
     $this->db->where('password', $password); 
     $query = $this->db->get(); 
     return $query->result(); 
    } 

我面臨的訪問問題上面顯示的$ userAuthenticated。它包含嘗試登錄的用戶的詳細信息。我如何訪問屬性?

問候,

+0

**我怎樣才能訪問屬性**這意味着你想訪問的信息駐留在這裏? –

+0

您的查詢應該只返回1條記錄,因此您應該使用 - > row()而不是 - > result()。 – ahmad

回答

0

取代這個

return $query->result(); 

與此:

if ($query->num_rows() == 1) { 
    return $query->row(); } 
    else { return FALSE; } 

替換此

$userAuthenticated = $this->Membership_model->userLogin($email, $pass); 

與此

// check negative condition first 
    if(! $data['userAuthenticated'] = $this->Membership_model->userLogin($email, $pass)){ 
    // show no results view 
    $this->load->view('wedontknowyouatall', $data); 
    } 
    else { 
    // we have a result back 
    // and we have already passed userAuthenticated into $data 
    // now show result view 
    $this->load->view('mysweetviewpage', $data); 
} 

在視圖頁面,您現在可以訪問所有的數據庫字段,如

echo $userAuthenticated->email ; 
+0

非常好,非常感謝您的支持 – spacemonkey

0
return $query->row(); 

if($userAuthenticated){ 
login true 
}else{ 
login false 
} 
0

我會檢查由這樣的查詢選擇的行數:獲得選擇

if($userAuthenticated->num_rows() > 0){ 
    login 
}else{ 
    not login 
} 

否則你必須做循環的數據:

foreach($userAuthenticated as $user){ 
    echo $user->[variable]; 
} 
0

只需使用

$ userAuthenticated-> firstname; $ userAuthenticated-> email; $ userAuthenticated-> pass;

等等。