mysqli还是死了,它一定要死吗? [英] mysqli or die, does it have to die?

查看:68
本文介绍了mysqli还是死了,它一定要死吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用一些这样的代码:

If I use a bit of code like this:

$update_result = mysqli_query( $link , $sql_update_login ) or die ('Unable to execute query. '. mysqli_error($link));

它必须死吗?还是可以在以后提出其他查询?就像预定的函数将错误日志写入另一个表一样?如:

Does it have to die or can you put a different query afterwards? Like a predetermined function that writes a log of the error to another table? Such as:

$update_result = mysqli_query( $link , $sql_update_login ) or function('$query, $error);

或"后还有哪些其他选择?我没有在文档中找到它,任何线索都值得赞赏.

What are the other options after 'or'? I haven't found it in the documentation, any clues are appreciated.

推荐答案

它必须死吗

Does it have to die

恰恰相反,永远不要die().
PHP是一种不良遗传的语言.遗传很差.并且or die()的错误消息是最糟糕的条件之一:

Quite contrary, it shouldn't die() ever.
PHP is a language of bad heredity. Very bad heredity. And or die() with error message being one of the worst rudiments:

  • die抛出错误消息,向潜在的攻击者透露了一些系统内部信息
  • 这会给无辜的用户带来奇怪的消息,使他们无法使用任何界面,因此他们很可能会退出.
  • 它会杀死中间的脚本,因此可能会导致显示的设计破损(或根本没有设计)(即用户请求的页面呈现不完整)
  • 杀死脚本无法恢复.虽然抛出异常可以被捕获并正常处理
  • die()不会提示您发生错误的地方.在相对较大的应用程序中,要找到它会很痛苦.
  • die throws an error message out, revealing some system internals to a potential attacker
  • it is confusing innocent users with strange messages and leaving them no interface to work with, so they'd likely just drop out.
  • it kills the script in the middle, so it may cause torn design (or no design at all) shown (i.e. an incomplete render of the page the user requested)
  • killing the script irrecoverably. While thrown exception can be caught and gracefully handled
  • die() gives you no hint of the place where error occurred. And in a relatively big application it will be quite a pain to find.

因此,千万不要将die()与mysql错误一起使用,即使是临时调试,也不要使用 :有更好的方法.

So, never use die() with mysql errors, even for the temporary debugging: there are better ways.

对于您的查询,您只有两种选择:

For your query you have but 2 choices:

  • 如果您打算在应用程序代码中完全使用mysqli_query()(这是错误的,但是在StackOverflow上您将永远不会学会其他方法),则可以使用trigger_error()代替die.它将引发常规的PHP错误,并将根据PHP设置自动记录.

  • if you are going to use mysqli_query() all the way in your application code (which is wrong but on StackOverflow you will never be taught any other way), you can use trigger_error() instead of die. It will raise a conventional PHP error and will be logged automatically, depending on PHP settings.

$result = mysqli_query($link , $sql) or trigger_error($link->error."[ $sql]");

  • 如果要使用mysqli_query()作为抽象库的一部分,则必须抛出一个新的Exception,因为您将需要一些堆栈跟踪(始终提供异常)来了解发生此错误的位置.

  • if you are going to use mysqli_query() as a part of abstraction library, you have to throw a new Exception instead, because you will need some stack trace (which always supplied with exception) to have an idea of where this error occurred.

    但是,不能将new ExceptionOR运算符一起使用.因此,代码变得更长一些了:

    However, you cannot use new Exception with the OR operator. So, the code becomes a little longer:

    $result = mysqli_query($link , $sql);
    if (!$result) {
        throw new Exception(mysqli_error($link)."[ $sql]");
    }
    

    这没什么大不了的,因为您只需编写一次即可.

    Which is not a big deal as you will have to write it only once.

    更新.事实证明, mysqli能够自行抛出异常,可以避免我们手动编写处理代码:

    Update. As it turned out, mysqli is able to throw exceptions by itself, which can relieve us from writing handling code manually:

    $result = mysqli_query($link, $sql);
    

    此代码在发生错误的情况下将引发异常,因此,您将始终在没有其他代码的情况下得到通知.但是,在前面的示例中,我们在错误消息中添加了一个SQL查询,这可能非常有价值-因此,人们也可以坚持上述方法.

    This code will throw an exception in case of error and thus you will always be informed without extra code. However, in previous examples we have an SQL query added to the error message, which can be quite valuable - so, one could stick to the aforementioned methods as well.

    重要提示

    将错误日志记录到另一个表的预定函数?

    a predetermined function that writes a log of the error to another table?

    这显然是一个坏主意.特别是如果您要将错误消息写到先前尝试失败的同一介质上.
    错误必须记录到最可靠的介质中-纯文本日志.因此,只需将您的PHP设置为编写错误日志并定期检查它们即可.

    This is apparently a bad idea. Especially if you want to write an error message to the same medium which failed previous attempt.
    Errors have to be logged into most robust medium - a plain text log. So, just set up your PHP to write error logs and check them regularly.

    这篇关于mysqli还是死了,它一定要死吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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