Bind Variable Mismatch in PHP's mysqli_stmt::bind_result()
When attempting to log in using a PHP login form with a prepared statement, you encounter the error:
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
Diagnosis:
This error occurs when the number of bind variables specified in bind_param() does not match the number of fields returned by the prepared statement.
Solution:
In your code:
$stmt->prepare("SELECT username AND password FROM users WHERE username = ?");
The prepared statement retrieves two fields, username and password, but your subsequent bind_param() and bind_result() calls specify only one bind variable, $username.
To resolve this, you need to correctly specify multiple bind variables and fields:
$mysqli->prepare("SELECT username, password FROM users WHERE username = ?"); $username = $_POST['name']; $stmt->bind_param('s', $username); $stmt->execute(); $stmt->bind_result($username, $password);
The above is the detailed content of Why Does `mysqli_stmt::bind_result()` Throw a Bind Variable Mismatch Error?. For more information, please follow other related articles on the PHP Chinese website!