2012-02-07 59 views
0

如果我把echo "h";放在if語句中,我什麼也沒有返回。另外,如果我在SQL代碼中放置一個WHERE子句並使用該方法,那麼在foreach循環中我也不會返回任何內容。任何想法爲什麼發生這種情況?我只需要一個登錄的當前用戶相匹配的用戶。什麼腳本的做的是通過在用戶表CodeIgniter查詢數據庫

public function dcr() { 
    // pass all dashboard accesses through this function 
    $username = $this->session->userdata("username"); 
    $query = $this->db->query("SELECT * FROM users"); 
    foreach ($query->result() as $row) { 
        echo "h"; 
     if ($username == $row->username) { 
       $data = array('firstname' => $row->firstname); 
       $this->load->view('dashboard', $data); 
      } 
    } 
} 
+0

表中是否有用戶? – 2012-02-07 01:44:14

+0

這取決於你的用戶表,你的用語在哪裏,用戶名是什麼等。發佈他們會幫助找到答案。 – 2012-02-07 01:45:11

+0

我在我的Ci控制器中設置了錯誤的會話。我現在開始工作了。 – 2012-02-07 02:11:21

回答

1

這應該做的伎倆(沒有測試)的所有行循環,你不需要if語句,只需使用函數get_where,因爲代碼將變得更清晰和可讀。

public function dcr() { 
    // pass all dashboard accesses through this function 
    $username = $this->session->userdata("username"); 
    $query = $this->db->get_where('users', array('username' => $username)); 
    foreach ($query->result() as $row) { 
     $data = array('firstname' => $row->firstname); 
     $this->load->view('dashboard', $data); 
    } 
} 
+0

謝謝你!我需要開始使用這些Ci方法。有人告訴我他們有安全的好處。 – 2012-02-07 14:15:38

+0

是的,不應該很難改變使用CI方法。 – Jasonw 2012-02-07 17:08:35