I'm trying to fetch some data from a statement. Below is the code and it works until bind_result()
. I have checked every other step only thing not working is the fetch()
. I tried to fix it for hours before posting here. I hope someone helps. Thanks in advance.
<?php
if (isset($_POST['login'])) {
// Get form inputs to variables
$username = strip_tags(trim($_POST['admin-email']));
$password = strip_tags(trim($_POST['admin-password']));
}
// If username and password is set
if (isset($username, $password)) {
// New db connection
$mysqli = new mysqli($servername, $dbusername, $dbuserpasswd, $dbname);
// Check db connection is set
if ($mysqli->connect_error) {
die("Database connection failed: " . $mysqli->connect_error);
}
// Prepare select query
$query = "SELECT admin_email, hashed_password FROM admins WHERE admin_email =?";
// Init statement
$stmt = $mysqli->stmt_init();
// Prepare statement
if (!$stmt->prepare($query)) {
print "Failed to prepare the statement";
}
else {
// Bind parameters to statement
$stmt->bind_param('s', $username);
// Execute statement
if (!$stmt->execute()) {
print "Failed to execute statement";
}
else {
// Get results to a variable
$results = $stmt->get_result();
// Check if user exists
if ($results->num_rows > 0) {
// Bind variables to prepared statement
$stmt->bind_result($username, $hashed_password);
// Fetch from statement
if (!$stmt->fetch()) {
print "Failed to fetch from statement";
}
else {
// Verifiy input password with hashed password on database
if (password_verify($password, $hashed_password)) {
echo "It's wrong!";
}
else {
echo "It's correct!";
}
}
}
}
}
}
?>
When you're binding variables to a statement you need to store results on the same statement instead of getting results to a different variable.
So instead
$results = $stmt->get_result();
Use below
$stmt->store_result();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.