CodeIgniter 模型无法 return 特定记录
CodeIgniter model unable to return specific records
我正在使用 CodeIgniter,但在使用 results()
方法从 table 中获取所有行时,我无法使 where()
选择方法起作用。
这是我的模型:
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();
}
它应该 return users
table 中与 $id
参数匹配的所有行,但相反,它只是获取 table 中的所有记录table,包括与提供的 $id
参数不匹配的那些。
我做错了什么?我试过 row()
方法,虽然它与 where()
一起使用,但它只 return 一行,所以它不适合我的情况。
问题是您调用了两次 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();
}
我正在使用 CodeIgniter,但在使用 results()
方法从 table 中获取所有行时,我无法使 where()
选择方法起作用。
这是我的模型:
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();
}
它应该 return users
table 中与 $id
参数匹配的所有行,但相反,它只是获取 table 中的所有记录table,包括与提供的 $id
参数不匹配的那些。
我做错了什么?我试过 row()
方法,虽然它与 where()
一起使用,但它只 return 一行,所以它不适合我的情况。
问题是您调用了两次 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();
}