Php mysqli bind_param变量数与预准备语句中的参数数量不匹配

时间:2013-02-08 20:45:43

标签: php mysqli prepared-statement

这必须是一个新手的错误,但我没有看到它。以下是我的代码片段:

$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的值时,我看到了值,但是当我运行此脚本时,我得到一个空值,并且我的日志文件中出现以下警告:

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)。我错过了什么吗?

1 个答案:

答案 0 :(得分:42)

你准备好的陈述是错的,应该是:

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

单引号是什么?是价值而不是标记。它已经是一个字符串,因为您使用bind_param('is'

进行投射