Saturday, June 30, 2018

mysqli - Php mysqi bind_param Number of variables doesn't match number of parameters in prepared statement




This has to be a newbie mistake, but I'm not seeing it. Here is a snippet from my code:



$mysqli = mysqli_connect($dbCredentials['hostname'], 
$dbCredentials['username'], $dbCredentials['password'],
$dbCredentials['database']);

if ($mysqli->connect_error) {
throw new exception( 'Connect Error (' . $mysqli->connect_errno . ') '

. $mysqli->connect_error);
}

$stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types
WHERE year = ? AND make = '?' ORDER by model");

$stmt->bind_param('is', $year, $make);

$stmt->execute();



When I echo out the values for $year and $make, I am seeing values, but when I run this script, I get a null value, and the following warning appears in my log file:



PHP Warning:  mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement


In this case, year is in the database in type int(10), and I have tried passing a copy that had been cast as an int, and make is a varchar(20) with the utf8_unicode_ci encoding. Am I missing something?


Answer



Your prepared statement is wrong, it should be:




$stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = ? ORDER by model");


The single quotes made that ? be the value not a marker. It will already be a string because you are casting as such with bind_param('is'


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...