MySQLi 准备语句错误报告
我正在尝试了解 MySQli,但我对错误报告感到困惑。 我使用 MySQLi 'prepare' 语句的返回值来检测执行 SQL 时的错误,如下所示:
$stmt_test = $mysqliDatabaseConnection->stmt_init();
if($stmt_test->prepare("INSERT INTO testtable VALUES (23,44,56)"))
{
$stmt_test->execute();
$stmt_test->close();
}
else echo("Statement failed: ". $stmt_test->error . "<br>");
但是,prepare 语句的返回值是否只检测 SQL 语句的准备中是否有错误而不检测执行错误?如果是这样,我是否应该更改我的执行行以标记错误,如下所示:
if($stmt_test->execute()) $errorflag=true;
然后为了安全起见,我还应该在语句执行后执行以下操作:
if($stmt_test->errno) {$errorflag=true;}
...或者我可以开始并返回值MySQLi 准备'语句捕获与其定义的查询的完整执行相关的所有错误?
谢谢 C
I'm trying to get my head around MySQli and I'm confused by the error reporting.
I am using the return value of the MySQLi 'prepare' statement to detect errors when executing SQL, like this:
$stmt_test = $mysqliDatabaseConnection->stmt_init();
if($stmt_test->prepare("INSERT INTO testtable VALUES (23,44,56)"))
{
$stmt_test->execute();
$stmt_test->close();
}
else echo("Statement failed: ". $stmt_test->error . "<br>");
But, is the return value of the prepare statement only detecting if there is an error in the preperation of the SQL statement and not detecting execution errors? If so should I therefore change my execute line to flag errors as well like this:
if($stmt_test->execute()) $errorflag=true;
And then just to be safe should I also do the following after the statement has executed:
if($stmt_test->errno) {$errorflag=true;}
...Or was I OK to start with and the return value on the MySQLi prepare' statement captures all errors associated with the complete execution of the query it defines?
Thanks
C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
mysqli 的每种方法都可能失败。幸运的是,现在 mysqli 可以向您报告所有问题,您所需要的只是询问。只需将这一行添加到连接代码中,
之后每个错误都会自行显现。无需测试任何返回值,只需立即编写语句:
当任何步骤发生错误时,它将抛出一个常见的 PHP 异常,可以像任何其他 PHP 错误一样处理或报告该异常。只需确保正确配置 PHP 错误报告,即在开发服务器上错误显示在屏幕上,而在生产服务器上错误从不显示而是记录下来。
Each method of mysqli can fail. Luckily, nowadays mysqli can report every problem to you, all you need is ask. Simply add this single line to the connection code,
And after that every error will reveal itself. No need to test any return values ever, just write your statements right away:
When the error occurs at any step, it will throw a usual PHP Exception that can be handled or just reported the same way as any other PHP error. Just make sure you configured PHP error reporting properly, i.e. on the dev server errors are displayed on-screen and on the production server errors are never displayed but logged instead.
完整性
您需要检查
$mysqli
和$statement
。如果为 false,则需要分别输出$mysqli->error
或$statement->error
。效率
对于可能终止的简单脚本,我使用简单的单行代码来触发消息中的 PHP 错误。对于更复杂的应用程序,应该激活错误警告系统,例如通过抛出异常。
使用示例 1:简单脚本
使用示例 2:应用程序
Completeness
You need to check both
$mysqli
and$statement
. If they are false, you need to output$mysqli->error
or$statement->error
respectively.Efficiency
For simple scripts that may terminate, I use simple one-liners that trigger a PHP error with the message. For a more complex application, an error warning system should be activated instead, for example by throwing an exception.
Usage example 1: Simple script
Usage example 2: Application
不确定这是否回答了您的问题。如果没有,抱歉
要从 mysql 数据库报告有关您的查询的错误,您需要使用连接对象作为焦点。
so:
将回显从 mysql 发送的有关您的查询的错误。
希望有帮助
Not sure if this answers your question or not. Sorry if not
To get the error reported from the mysql database about your query you need to use your connection object as the focus.
so:
would echo the error being sent from mysql about your query.
Hope that helps