2015-12-02 71 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?真的很感激。 –