我有一个登录网站,用户可以使用他们的电子邮件登录。每个都分配有一个自动递增的 user_id。我想从数据库中获取关联的 user_id 并将其存储在 session 中,以便当他们提交内容时,可以从 session 中获取他们的 user_id 并与内容一起提交。但是,我的“get_user_id”函数无法从用户的电子邮件地址查找用户 ID。我尝试过使用 fetch_row、get_result、bind_result 中的各种 mysqli 命令,但没有任何结果会返回 user_id。
function get_user_id($email) {
//$email = mysqli_real_escape_string($this->conn, $email);
$query = "SELECT id from users where email=?";
if ($stmt = $this->conn->prepare($query)){
$stmt->bind_param('s',$email);
if($result = $stmt->execute()){
$row=$result->fetch_row();
return $row['id'];
} else return "no ID returned.";
}
}
我现在收到的错误消息是:
Fatal error: Call to a member function fetch_row() on a non-object
最佳答案
变量$result
将包含一个 bool 值
- TRUE或FALSE。
您尝试对该 bool 值调用函数 fetch_row()
,但该函数不起作用。
如果您使用准备好的语句,则应使用 bind_result()
,然后使用 $stmt
上的 fetch()
函数。
将 $row=$result->fetch_row();
更改为 $stmt->fetch();
,而不是使用变量 $row
,调用 bind_result()
,如此处示例中所述:
http://php.net/manual/en/mysqli-stmt.fetch.php
关于php - Mysqli 代码获取一个结果并返回它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793916/