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

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

问题描述

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

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语句的前置是否存在错误,而不检测执行错误?如果是这样,我应该更改执行行以标记错误,如下所示:

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?

谢谢 C

推荐答案

我在过去两天里写了两次(对我来说,这是重复的,即使问题开始有所不同).

I wrote this twice before in the last two days (so for me it's a duplicate even though the questions started a bit different).

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扩展完全能够报告通过异常导致除0以外的(mysqli)错误代码的操作,请参见 die()确实非常粗糙,即使在这样的示例中,我也不会使用它.
因此,请仅考虑以下事实:每个(mysql)操作 可能由于多种原因而失败;甚至如果完全一样的事情在...之前就进行了一千次....

edit: 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天全站免登陆