Home >Backend Development >PHP Tutorial >Why Does `mysqli_stmt::bind_result()` Throw a 'Binding Mismatch' Error in PHP Login Forms?
Mysqli_stmt::bind_result() Error: Binding Mismatch
When attempting to implement a PHP login form using a prepared statement, users may encounter the following error:
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
This error indicates a discrepancy between the number of variables specified in bind_result() and the number of fields returned by the prepared statement. To resolve this issue, examine the following code snippet:
$mysqli = new mysqli("localhost", "root", "", "security"); $stmt = $mysqli->prepare("SELECT username AND password FROM users WHERE username = ?"); $username = $_POST['name']; $stmt->bind_param('s', $username); $stmt->execute(); $stmt->bind_result($password, $username); $stmt->fetch();
Root Cause:
The incorrect syntax in the SELECT statement is the root cause of the error. The correct syntax should be:
SELECT username, password FROM users WHERE username = ?
Resolution:
The issue can be resolved by separating the fields in the SELECT statement with a comma instead of "AND." The correct code is provided below:
$mysqli->prepare("SELECT username, password FROM users WHERE username = ?");
By ensuring the correct syntax in the SELECT statement and observing the number of bind variables matches the number of fields in the prepared statement, the mysqli_stmt::bind_result() error can be prevented.
The above is the detailed content of Why Does `mysqli_stmt::bind_result()` Throw a 'Binding Mismatch' Error in PHP Login Forms?. For more information, please follow other related articles on the PHP Chinese website!