問題描述
這一定是新手的錯誤,但我沒有看到.這是我的代碼片段:
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 變量數與準備好的語句中的參數數不匹配的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!