Understanding the 'Number of Variables Doesn't Match Number of Parameters' Error in mysqli_bind_param()
When using prepared statements with MySQL's mysqli extension, it's crucial to ensure that the binding of parameters aligns with the prepared query syntax. Failure to do so can result in an error like "Number of variables doesn't match number of parameters in prepared statement."
Let's delve deeper into the provided code snippet to identify the issue:
$stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = '?' ORDER by model"); $stmt->bind_param('is', $year, $make); $stmt->execute();
The error arises because the prepared statement contains a placeholder for the make value enclosed in quotes: "?'". In a prepared statement, question marks are used as placeholders for values that will be bound later. However, when they are enclosed in quotes, they are treated as literal text instead of placeholders.
Correcting the Prepared Statement
To fix the issue, remove the quotes surrounding the ? placeholder for make in the prepared statement:
$stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = ? ORDER by model"); $stmt->bind_param('is', $year, $make); $stmt->execute();
Now, the prepared statement has one ? placeholder for each variable, year and make.
Key Points to Remember
The above is the detailed content of How to Troubleshoot \'Number of Variables Doesn\'t Match Number of Parameters\' Issue in mysqli_bind_param()?. For more information, please follow other related articles on the PHP Chinese website!