Home >Database >Mysql Tutorial >Why Does `mysqli_stmt::bind_result()` Throw a 'Number of Bind Variables Mismatch' Error in PHP?
mysqli_stmt::bind_result(): Number of Bind Variables Mismatch
When attempting to implement a PHP login form using a prepared statement, users commonly encounter the error "mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement." This error signifies a discrepancy between the number of bind variables used to store query results and the actual number of fields returned by the underlying SQL statement.
Diagnostic Code
$stmt = $mysqli->prepare("SELECT username, password FROM users WHERE username = ?"); $username = $_POST['name']; $stmt->bind_param('s', $username); $stmt->execute(); $stmt->bind_result($username, $password); // Incorrect field binding $stmt->fetch();
Correction
The code above contains a mistake in its field bindings. The bind_result() method should reflect the actual fields returned by the SELECT statement. In this scenario, the query returns two fields: username and password. Therefore, the correct code is:
$stmt->bind_result($username, $password); // Corrected field binding
Further Tips
The above is the detailed content of Why Does `mysqli_stmt::bind_result()` Throw a 'Number of Bind Variables Mismatch' Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!