php Mysqli Prepare 语句 - 返回 False,但为什么呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1219809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysqli Prepare Statement - Returning False, but Why?
提问by Bill Karwin
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):
我有一个函数,它根据要插入到该列中的列名和值的关联数组以及一个表名(一个简单的字符串)生成一个准备好的 INSERT 语句:
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;
}
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!
我的问题是 $this->conn->prepare(它是一个类的一部分,conn 是一个新的 mysqli 对象,它没有问题)返回 false,但没有给我一个原因!
Here is a sample $sqlString that gets built for the prepare statement:
这是为准备语句构建的示例 $sqlString:
INSERT INTO students (PhoneNumber, FirstName, MiddleInit, LastName, Email, Password, SignupType, Active, SignupDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
Can anyone see a problem with this parameterized statement? Any reason the prepare function would return false?
任何人都可以看到这个参数化语句的问题吗?任何原因准备函数会返回false?
回答by Bill Karwin
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.
我正在将解决方案复制到此答案中,以便可以对此进行投票,否则该问题将永远出现在“未回答的问题”中。我将这个答案标记为 CW,所以我不会得到任何积分。
@Andrew E. says:
@Andrew E. 说:
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_report(MYSQLI_REPORT_ALL)为了更好地了解正在发生的事情 -结果我的一个字段名称不正确- 你会认为这prepare()会引发异常,但它默默地失败了。

