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

Php mysqi bind_param Number of variables doesn#39;t match number of parameters in prepared statement(Php mysqi bind_param 变量数与准备好的语句中的参数数不匹配)
本文介绍了Php mysqi bind_param 变量数与准备好的语句中的参数数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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 变量数与准备好的语句中的参数数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?(警告:mysqli_query() 需要至少 2 个参数,1 个给定.什么?)
INSERT query produces quot;Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean givenquot;(INSERT 查询产生“警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,给出布尔值;)
prepared statements - are they necessary(准备好的陈述 - 它们是否必要)
Do I need to escape my variables if I use MySQLi prepared statements?(如果我使用 MySQLi 准备好的语句,是否需要转义我的变量?)
Properly Escaping with MySQLI | query over prepared statements(使用 MySQLI 正确转义 |查询准备好的语句)
Is it possible to use mysqli_fetch_object with a prepared statement(是否可以将 mysqli_fetch_object 与准备好的语句一起使用)