我正在尝试实现一个登录系统,除了这个返回 0 的 PHP 脚本外,它主要工作:
// $username_err & $password_err is empty, gets reset every time as "".
if(empty($username_err) && empty($password_err)){
$query = "SELECT rowid, username, password FROM admin_login WHERE username = ?";
if($stmt = mysqli_prepare($db, $query)){
mysqli_stmt_bind_param($stmt, "s", $param_username);
$param_username = $username;
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
// Doesn't work here, works up until here.
if(mysqli_stmt_num_rows($stmt) == 1){
mysqli_stmt_bind_result($stmt, $rowid, $username, $hashed_password);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
session_start();
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $rowid;
$_SESSION["username"] = $username;
header("location: index.php");
}else{
$password_err = "Invalid password";
}
}
}else{
$username_err = "No such account exists.";
}
}else{
echo "An error occurred.";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($db);
这永远不会奏效,因为它卡住了,因为它不满足条件 mysqli_stmt_num_rows == 1,而是返回 0。它确实通过 MySQL 直接通过 Workbench 工作:
SELECT rowid, username, password FROM admin_login WHERE username = "admin";
确实返回具有匹配条件的 1 行。
** 编辑:这是我的新代码;**
<?php
require_once "_php/login/config.php";
$username = $password = "";
$err = "";
它抛出:
致命错误:未捕获错误:在目录/login_script.php:25 中的布尔值上调用成员函数 fetch_assoc()堆栈跟踪:#0目录/login.php(2): require() #1 {main} 抛出目录/第 25 行的 login_script.php
作为参考,login_script 是PHP 脚本,login 是php 的前端。
潇湘沐