以下codeigniter函数使用(string)参数并返回该行的(整数)ID。它工作正常,如果我传递字符串值,但如果传递整数0,它会返回数据库中第一行的ID。原则上,只有在数据库中存在user_name
时,才会返回user_id
。由于没有user_name称为0,它应该返回false。 有人可以告诉为什么它的行为如何以及如何修复?关于PHP函数返回的问题
谢谢。
public function get_user_id($user_name)
{
$this -> db -> select('user_id');
$this -> db -> from('users');
$this -> db -> where('user_name', $user_name);
$this -> db -> limit(1);
$query = $this->db->get();
if ($query->num_rows > 0)
{
$row = $query->row();
return $row->user_id;
}
return false;
}
例如:
$user_name = "test"; //works fine, returns id.
$user_name = "0"; //works fine, doesnt return anything
$user_name = 0; //Problem. returns ID of first row.
问题出在生成的sql语句上,你需要将它作为字符串传递给WHERE() – Gerep