Php mysqi bind_param 变量数与准备好的语句中的参数数不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14781051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php mysqi bind_param Number of variables doesn't match number of parameters in prepared statement
提问by TMorgan
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:
当我回显 $year 和 $make 的值时,我看到的是值,但是当我运行这个脚本时,我得到一个空值,并且我的日志文件中出现以下警告:
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?
在这种情况下,year 在数据库中的类型为 int(10),我尝试传递一个已转换为 int 的副本,而 make 是具有 utf8_unicode_ci 编码的 varchar(20)。我错过了什么吗?
回答by runspired
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'
单引号做到了?是价值而不是标记。它已经是一个字符串,因为您正在使用bind_param('is'

