警告:mysqli_stmt_bind_result():绑定变量的数量与准备语句中的字段数量不匹配
Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement
我正在做一个登录表单,由于我昨天刚刚了解了准备好的语句,所以我被代码困住了。我不断收到此错误:
Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement.
我需要一些关于如何使用该功能的启发。我只包含了与我的问题相关的代码。
// Validate credentials
if(empty($email_err) && empty($password_err) && empty($userLevel_err)){
// Prepare a select statement
$sql = "SELECT id, email, password FROM users WHERE email = ? AND userLevel=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_userLevel);
// Set parameters
$param_email = $email;
$param_userLevel = $userLevel;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if email exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password, $userLevel);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
// Password is correct, so start a new session
session_start();
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["email"] = $email;
// Redirect user to welcome page
header("location: index.php");
} else{
// Display an error message if password is not valid
$password_err = "The password you entered was not valid.";
}
问题是绑定变量的数量与准备语句中的字段数量不匹配。您要么需要绑定更少的变量,要么需要将缺少的第四列添加到 SQL
// VVV Add the fourth column here
$sql = "SELECT id, email, password, userLevel FROM users WHERE email = ? AND userLevel=?";
或者绑定更少的变量
// VVV Remove the fourth binding
mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password);
我正在做一个登录表单,由于我昨天刚刚了解了准备好的语句,所以我被代码困住了。我不断收到此错误:
Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement.
我需要一些关于如何使用该功能的启发。我只包含了与我的问题相关的代码。
// Validate credentials
if(empty($email_err) && empty($password_err) && empty($userLevel_err)){
// Prepare a select statement
$sql = "SELECT id, email, password FROM users WHERE email = ? AND userLevel=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_userLevel);
// Set parameters
$param_email = $email;
$param_userLevel = $userLevel;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if email exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password, $userLevel);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
// Password is correct, so start a new session
session_start();
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["email"] = $email;
// Redirect user to welcome page
header("location: index.php");
} else{
// Display an error message if password is not valid
$password_err = "The password you entered was not valid.";
}
问题是绑定变量的数量与准备语句中的字段数量不匹配。您要么需要绑定更少的变量,要么需要将缺少的第四列添加到 SQL
// VVV Add the fourth column here
$sql = "SELECT id, email, password, userLevel FROM users WHERE email = ? AND userLevel=?";
或者绑定更少的变量
// VVV Remove the fourth binding
mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password);