2015-12-02 48 views
1

我使用的是CodeIgniter,當使用results()方法從表中提取所有行時,我無法使where()選擇方法起作用。CodeIgniter模型無法返回特定記錄

這裏是我的模型:

public function get_all_entries($id) 
    { 
     // Select row to be fetched 
     $this->db->where('id', $id); 
     $this->db->get('users'); 
     // Execute the find query with provided data 
     $query = $this->db->get('users'); 
     // Return an object with all the data 
     return $query->result(); 
    } 

它應該返回所有匹配的users$id的說法,但相反,它只是取表中的所有記錄,包括那些沒有按行與提供的$id參數不匹配。

我在做什麼錯在這裏?我試過row()方法,雖然它與where()一起使用,但它只返回一行,所以它不適合我的情況。

回答

1

問題是你要調用get()方法兩次,第一次使用where,但沒有賦值給變量;第二個被分配給一個變量,但由於where子句已經被另一個使用,它就獲取了一切。刪除第一個get,你應該沒問題。

public function get_all_entries($id) 
{ 
    // Select row to be fetched 
    $this->db->where('id', $id); 
    // Execute the find query with provided data 
    $query = $this->db->get('users'); 
    // Return an object with all the data 
    return $query->result(); 
} 
+0

我試過了,但是還有一個問題(如果ID是在表零與否),導致我覺得這也不是辦法,現在我固定的問題,解決你提到的作品,謝謝。 –

+0

很高興它的工作。 –

+0

您能否提出問題upvote?真的很感激。 –