[英]How to assign values to a variable with bind_result and fetch?
I want to get values from my database and assign them to a variable. 我想从数据库中获取值并将其分配给变量。 This is my code. 这是我的代码。
$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type);
$user = $stmt->fetch();
$stmt->close();
if($encrypted_password == $password){
return $user;
}
} else {
return NULL;
}
i need to assign the values from my table to the variable $user and return it to my login.php file. 我需要将表中的值分配给变量$ user并将其返回到我的login.php文件。
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// use is found
$response["error"] = FALSE;
$response["ngno"] = $user["ngno"];
$response["user"]["email"] = $user["email"];
$response["user"]["name"] = $user["name"];
$response["user"]["user_type"] = $user["user_type"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
my code doesnt seem to assign values into the $user variable. 我的代码似乎没有将值分配给$ user变量。 what am i doing wrong? 我究竟做错了什么?
Since I cannot put a comment, have you tried: 由于我无法发表评论,您是否尝试过:
var_dump($user);
If it returns NULL, check your method getUserByEmailAndPassword, especially this line: 如果返回NULL,请检查您的方法getUserByEmailAndPassword,尤其是以下行:
$encrypted_password == $password
Since your are comparing your $encrypted_password to the $password variable, are you sure that the $password variable you are comparing to is also the encrypted (should be hashed) password? 由于您正在将$ encrypted_password与$ password变量进行比较,因此您确定要与之比较的$ password变量也是加密(应该散列)的密码吗? Will be updating this answer once you respond. 回复后,将更新此答案。
The problem is because of the following statement in getUserByEmailAndPassword()
method, 问题是由于getUserByEmailAndPassword()
方法中的以下语句,
$user = $stmt->fetch();
fetch()
method fetches results from a prepared statement into the bound variables, and the method actually returns true
or false
. fetch()
方法将准备好的语句的结果提取到绑定变量中,并且该方法实际上返回true
或false
。
Here's the reference: 这是参考:
Instead create an array, say $user
and push those bound variable values into the array. 而是创建一个数组,比如说$user
然后将那些绑定变量值推入该数组。 And finally return the array from the method. 最后从方法返回数组。
So your getUserByEmailAndPassword()
method should be like this: 因此,您的getUserByEmailAndPassword()
方法应如下所示:
public function getUserByEmailAndPassword($email, $password){
$stmt = $this->conn->prepare("SELECT ngno, email, encrypted_password, name, user_type FROM `guide` WHERE email = ? LIMIT 1");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$stmt->bind_result($ngno, $email, $encrypted_password, $name, $user_type);
$user = array();
if($stmt->fetch()){
if($encrypted_password == $password){
$user['ngno'] = $ngno;
$user['email'] = $email;
$user['name'] = $name;
$user['user_type'] = $user_type;
$stmt->close();
return $user;
}else{
return false;
}
}else{
return false;
}
} else {
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.