問題描述
我有一個函數,它根據要插入到該列中的列名和值的關聯數組以及一個表名(一個簡單的字符串)生成一個準備好的 INSERT 語句:
I have a function that generates a prepared INSERT statement based on an associative array of column names and values to be inserted into that column and a table name (a simple string):
function insert ($param, $table) {
$sqlString = "INSERT INTO $table (".implode(', ',array_keys($param)).') VALUES ('.str_repeat('?, ', (count($param) - 1)).'?)';
if ($statement = $this->conn->prepare($sqlString)):
$parameters = array_merge(array($this->bindParams($param), $param));
call_user_func_array(array($statement, 'bind_param', $parameters));
if (!$statement->execute()):
die('Error! '.$statement->error());
endif;
$statement->close();
return true;
else:
die("Could Not Run Statement");
endif;
}
我的問題是 $this->conn->prepare(它是一個類的一部分,conn 是一個新的 mysqli 對象,它沒有問題)返回 false,但沒有給我一個原因!
My problem is that $this->conn->prepare (it's part of a class, conn is a NEW mysqli object, which works with no issues) returns false, but does not give me a reason why!
這是為準備語句構建的示例 $sqlString:
Here is a sample $sqlString that gets built for the prepare statement:
INSERT INTO students (PhoneNumber, FirstName, MiddleInit, LastName, Email, Password, SignupType, Active, SignupDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
有人能看出這個參數化語句有什么問題嗎?準備函數返回 false 的任何原因?
Can anyone see a problem with this parameterized statement? Any reason the prepare function would return false?
推薦答案
我正在將解決方案復制到此答案中,以便可以對此進行投票,否則該問題將永遠出現在未回答的問題"中.我將這個答案標記為 CW,所以我不會得到任何分數.
I'm copying the solution into this answer so this can be given an upvote, otherwise the question will appear in the "unanswered questions" forever. I'm marking this answer CW so I won't get any points.
@Andrew E. 說:
@Andrew E. says:
我剛打開mysqli_report(MYSQLI_REPORT_ALL)
到更好地了解什么是繼續 - 結果是我的一個字段名稱不正確 - 你會認為 prepare()
會拋出一個異常,但它默默地失敗了.
I just turned on
mysqli_report(MYSQLI_REPORT_ALL)
to get a better understanding of what was going on - turns out that one of my field names was incorrect - you'd think thatprepare()
would throw an exception, but it fails silently.
這篇關于Mysqli Prepare 語句 - 返回 False,但為什么呢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!