我在这里从数据库中检索信息时遇到了一些问题。如何检索用户尝试登录的ID但$ id变量似乎是空白而其他两个返回正确的值和想法。
// check if the user id and password combination exist in database
$q = "SELECT * FROM users WHERE email='".$user."' AND password='".$pass."'";
// Run Query
$result = $li->query($q) or die(mysqli_error($mysqli));
// Numbers of rows that match
$num_rows = $result->num_rows;
echo $num_rows;
// Check if active status
$active = $result->fetch_object()->active;
echo $active;
$id = $result->fetch_object()->id;
echo $id;
答案 0 :(得分:1)
最后一次获取是空白的,因为fetch_object将光标向前移动,所以在第二个fetch_object上你实际上正在获取不存在的下一个对象。将您的代码更改为:
$obj = $result->fetch_object();
echo $obj->active;
echo $obj->id;