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

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

问题描述

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

推荐答案

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

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