35

这一定是新手的错误,但我没有看到。这是我的代码片段:

$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

在这种情况下,数据库中的年份是 int(10) 类型,我尝试传递一个已被转换为 int 的副本,并且 make 是一个 varchar(20) 与 utf8_unicode_ci 编码。我错过了什么吗?

4

1 回答 1

58

您准备的陈述是错误的,应该是:

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

单引号造成的?是值而不是标记。它已经是一个字符串,因为您正在使用bind_param('is'

于 2013-02-08T20:58:28.350 回答