php mysqi bind_param变量数与准备好的语句中的参数数不匹配 [英] 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();
当我回显$ year和$ make的值时,我看到的是值,但是当我运行此脚本时,我得到一个空值,并且在我的日志文件中出现以下警告:
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
在这种情况下,year在数据库中的类型为int(10),我尝试传递一个已转换为int的副本,make是具有utf8_unicode_ci编码的varchar(20).我想念什么吗?
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?
推荐答案
您准备好的语句是错误的,应该是:
Your prepared statement is wrong, it should be:
$stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = ? ORDER by model");
用单引号引起的?是值而不是标记.因为已经使用bind_param('is'
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'
这篇关于php mysqi bind_param变量数与准备好的语句中的参数数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!