MySQLi 准备语句错误报告 [英] MySQLi prepared statements error reporting

查看:27
本文介绍了MySQLi 准备语句错误报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 MySQli,但对错误报告感到困惑.我在执行 SQL 时使用 MySQLi 'prepare' 语句的返回值来检测错误,如下所示:

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>");

但是,prepare 语句的返回值是不是只检测SQL 语句的preperation 是否有错误,而不检测执行错误?如果是这样,我应该更改我的执行行以标记错误,如下所示:

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;}

...或者我是否可以开始并且 MySQLi prepare' 语句的返回值捕获与其定义的查询的完整执行相关的所有错误?

...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?

谢谢

推荐答案

mysqli 的每个方法都可能失败.您应该测试每个返回值.如果失败,请考虑继续处理未处于您预期状态的对象是否有意义.(可能不是处于安全"状态,但我认为这不是问题.)

Each method of mysqli can fail. You should test each return value. If one fails, think about whether it makes sense to continue with an object that is not in the state you expect it to be. (Potentially not in a "safe" state, but I think that's not an issue here.)

由于每个连接/语句只存储最后一次操作的错误消息,如果出现问题后继续操作,您可能会丢失有关什么导致错误的信息.您可能希望使用该信息让脚本决定是重试(只是临时问题)、更改某些内容还是完全退出(并报告错误).它使调试变得更加容易.

Since only the error message for the last operation is stored per connection/statement you might lose information about what caused the error if you continue after something went wrong. You might want to use that information to let the script decide whether to try again (only a temporary issue), change something or to bail out completely (and report a bug). And it makes debugging a lot easier.

$stmt = $mysqli->prepare("INSERT INTO testtable VALUES (?,?,?)");
// prepare() can fail because of syntax errors, missing privileges, ....
if ( false===$stmt ) {
  // and since all the following operations need a valid/ready statement object
  // it doesn't make sense to go on
  // you might want to use a more sophisticated mechanism than die()
  // but's it's only an example
  die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}

$rc = $stmt->bind_param('iii', $x, $y, $z);
// bind_param() can fail because the number of parameter doesn't match the placeholders in the statement
// or there's a type conflict(?), or ....
if ( false===$rc ) {
  // again execute() is useless if you can't bind the parameters. Bail out somehow.
  die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}

$rc = $stmt->execute();
// execute() can fail for various reasons. And may it be as stupid as someone tripping over the network cable
// 2006 "server gone away" is always an option
if ( false===$rc ) {
  die('execute() failed: ' . htmlspecialchars($stmt->error));
}

$stmt->close();

六年后的一些笔记......

mysqli 扩展完全能够报告通过异常导致 (mysqli) 错误代码而不是 0 的操作,请参阅 mysqli_driver::$report_mode.
die() 真的非常粗糙,我不会再将它用于这样的例子了.
因此,请忽略每一个(mysql)操作可能由于多种原因而失败的事实;即使如果完全相同的事情在之前一千次都成功....

Just a few notes six years later...

The mysqli extension is perfectly capable of reporting operations that result in an (mysqli) error code other than 0 via exceptions, see mysqli_driver::$report_mode.
die() is really, really crude and I wouldn't use it even for examples like this one anymore.
So please, only take away the fact that each and every (mysql) operation can fail for a number of reasons; even if the exact same thing went well a thousand times before....

这篇关于MySQLi 准备语句错误报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆