调用“mysqli_stmt_prepare"时是否应该手动检查错误? [英] Should I manually check for errors when calling "mysqli_stmt_prepare"?

查看:26
本文介绍了调用“mysqli_stmt_prepare"时是否应该手动检查错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP 和 mysqli 准备好的语句.执行 mysqli_stmt_prepare() 时是否有令人信服的理由手动检查错误?更具体地说,我不是在问准备语句行的最终结果.

I'm using PHP and mysqli prepared statements. Is there a compelling reason to manually check for errors when executing mysqli_stmt_prepare()? To be more specific I am not asking about the final result just the prepare statement line.

$sql = "SELECT * FROM `users`;";
$stmt = mysqli_stmt_init($db);
mysqli_stmt_prepare($stmt, $sql); // How should I check for error in here
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

PHP 手册 将这一行和仅这一行放入if 语句.

PHP manual puts this and only this line in an if statement.

$sql = "SELECT * FROM `users`;";
$stmt = mysqli_stmt_init($db);
if (mysqli_stmt_prepare($stmt, 'SELECT * FROM `users`;')) {
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
}

我想知道在 mysqli 中使用准备好的语句时如何正确检查错误.是否有充分的理由手动检查手册中显示的该函数的返回值?

I would like to know how to properly check for errors when using prepared statements in mysqli. Is there a good reason to manually check the return value of that function as it is shown in the manual?

推荐答案

是否有充分的理由手动检查它的返回值功能如手册中所示?

不,没有.

Mysqli 可以自动检查错误,你只需要要求它这样做.
配置mysqli每次出错就抛出异常,再也不用手动去检查mysqli的任何函数了!

Mysqli can check for errors automatically, you just have to ask it to do so.
Configure mysqli to throw an exception every time it gets an error, and you won't have to check any mysqli function for the error manually anymore!

因此,在 mysqli_connect()

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

仅此而已!

请注意,您必须以正确的方式处理可能出现的错误.您可以在我的文章 PHP 错误报告

Note that you have to deal with possible errors the right way. You can read about it in my article, PHP error reporting

这篇关于调用“mysqli_stmt_prepare"时是否应该手动检查错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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