我正在使用 CodeIgniter,但在使用 results()
方法从表。
这是我的模型:
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()
一起工作,但它只返回一行,所以它不适合我的情况。
最佳答案
问题是您调用了两次 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();
}
关于php - CodeIgniter 模型无法返回特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34049065/