Number of bind variables never matching up

Everytime this runs I get the following error, but everything should be exactly the same count.

[COLOR=#000000][FONT=Times New Roman]Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables[/FONT][/COLOR]

Here is the code I have…

$str_query = 'INSERT INTO People (FirstName, LastName, Email) VALUES (?, ?, ?)'; // 3 question marks
$str_bindingTypes = 'sss'; // 3 types
$arr_values = array('Bob', 'Jones', 'bjones@gmail.com'); // 3 values

if ($statement = $this->mysqli->prepare($str_query)) {
     $statement->bind_param($str_bindingTypes, $arr_values);
     $statement->execute();
}

Any ideas?

That’s because your bind_param call is passing two parameters (the second an array) where the call expects four parameters.

Ahh… I read their documentation and thought you could put an array with the values in there or just keep adding values separately. I got my hopes up on something slightly easier. Thanks!

If you’re using PHP 5.6, then you can put an ellipse before the array to unpack it and pass the elements of the array as individual arguments :wink: See the variadics RFC for more information!

1 Like

on the other hand side, PDO supports that kind of functionality (passing in an array of parameters, though only for string types, but you could also incrementally add your parameters as well).