库MySQLi准备语句错误报告[复制]库MySQLi准备语句错误报告[复制](MySQLi prep

2019-05-09 09:06发布

这个问题已经在这里有一个答案:

  • 如何获得的MySQLi错误信息在不同的环境下 1个回答

我试图让我的周围mysqli的头,我被错误报告混淆。 我使用的库MySQLi的返回值执行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>");

不过,是准备语句的返回值仅检测是否存在SQL语句的水面浮油的错误,而不是检测执行错误? 如果是这样,我应该因此改变我的执行线来标记错误,以及这样的:

if($stmt_test->execute()) $errorflag=true;

然后,为了安全起见,我应该也做了以下语句已被执行之后:

if($stmt_test->errno) {$errorflag=true;}

...或者是我确定,开始和在库MySQLi的返回值准备”语句捕捉它定义查询的完整执行相关联的所有错误?

由于ç

Answer 1:

我在过去两天之前两次写了这个(所以对我来说这是一个重复的,即使问题开始有点不同)。

库MySQLi的每个方法可能会失败。 您应该测试每一个返回值。 如果一个人失败了,仔细想想是否有意义继续与一个对象,是不是在你希望它是国家。 (可能不处于“安全”状态,但我认为这里不是一个问题。)

因为只有在最后操作的错误信息被存储在每个连接/声明你可能会失去约,如果你继续后出了问题是什么导致了错误的信息。 您可能需要使用这些信息来让脚本决定是否再次尝试(只是暂时的问题),改变的东西,或完全摆脱困境(和报告错误)。 它使调试容易得多。

$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 。
死亡()是真的,真的很粗,我不会使用它,即使对于像这样的例子了。
所以,请只带走的事实, 每一个 (MySQL的)操作可能会因多种原因; 即使同样的事情进行得很顺利前一千倍....



Answer 2:

完整性

您需要同时检查$mysqli$statement 。 如果他们是假的,你需要输出$mysqli->error$statement->error分别。

效率

对于简单的脚本,可以随时终止,我用简单的单行触发一个PHP的错误与消息。 对于更复杂的应用程序,错误预警系统应该代替抛出一个异常激活,例如。

用法示例1:简单的脚本

# This is in a simple command line script
$mysqli = new mysqli('localhost', 'buzUser', 'buzPassword');
$q = "UPDATE foo SET bar=1";
($statement = $mysqli->prepare($q)) or trigger_error($mysqli->error, E_USER_ERROR);
$statement->execute() or trigger_error($statement->error, E_USER_ERROR);

使用实施例2:应用

# This is part of an application
class FuzDatabaseException extends Exception {
}

class Foo {
  public $mysqli;
  public function __construct(mysqli $mysqli) {
    $this->mysqli = $mysqli;
  }
  public function updateBar() {
    $q = "UPDATE foo SET bar=1";
    $statement = $this->mysqli->prepare($q);
    if (!$statement) {
      throw new FuzDatabaseException($mysqli->error);
    }

    if (!$statement->execute()) {
      throw new FuzDatabaseException($statement->error);
    }
  }
}

$foo = new Foo(new mysqli('localhost','buzUser','buzPassword'));
try {
  $foo->updateBar();
} catch (FuzDatabaseException $e)
  $msg = $e->getMessage();
  // Now send warning emails, write log
}


Answer 3:

不知道这是否回答您的问题或没有。 很抱歉,如果不

为了从你的查询,你需要使用连接对象为重点的mysql数据库报告的错误。

所以:

echo $mysqliDatabaseConnection->error

要重申的错误从MySQL发送有关查询。

希望帮助



文章来源: MySQLi prepared statements error reporting [duplicate]