php mysqli 或死,它必须死吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15318368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:02:41  来源:igfitidea点击:

mysqli or die, does it have to die?

phpmysqlmysqli

提问by Maelish

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.

“或”之后的其他选项是什么?我没有在文档中找到它,任何线索表示赞赏。

回答by Your Common Sense

Does it have to die

必须死吗

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

恰恰相反,它不应该die()永远。
PHP 是一种遗传不良的语言。非常不好的遗传。并且or 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 the error occurred. And in a relatively big application it will be quite a pain to find.
  • die 抛出错误消息,向潜在攻击者透露一些系统内部信息
  • 奇怪的消息让无辜的用户感到困惑,让他们没有界面可以使用,所以他们很可能会退出。
  • 它会在中间杀死脚本,因此可能会导致显示的设计撕裂(或根本没有设计)(即用户请求的页面呈现不完整)
  • 不可恢复地杀死脚本。虽然抛出的异常可以被捕获并优雅地处理
  • die()没有提示发生错误的地方。而在一个相对较大的应用程序中,它会很难找到。

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

所以,永远不要使用die()MySQL 错误,即使是临时调试:有更好的方法。

Instead of manually checking for the error, just configure mysqli to throw exceptions on error, by adding the following line to your connection code

无需手动检查错误,只需在连接代码中添加以下行,将 mysqli 配置为在出错时抛出异常

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

and after that just write every mysqli command as is, without any or dieor anything else:

然后按原样编写每个 mysqli 命令,没有任何or die其他命令:

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

This code will throw an exception in case of error and thus you will always be informed of every problem without a single line of extra code.

如果出现错误,此代码将抛出异常,因此您将始终知道每个问题,而无需一行额外的代码。

回答by Blender

oris just an operator (very similar to ||).

or只是一个运算符(非常类似于||)。

The or die()syntax works because orshort-circuits, which means that if the first statement is true, True or Xwill always be true, so Xisn't evaluated and your script doesn't die.

or die()语法工作,因为or短路,这意味着如果第一个说法是正确的,True or X永远是真实的,所以X不评价,你的脚本没有die

回答by kalaero

Yes, you can provide a different function after the (or). I have tested the following:

是的,您可以在(或)之后提供不同的功能。我已经测试了以下内容:

mysqli_query($sel_db,'what!') or some_func(mysqli_error($sel_db));

function some_func($str) {
    die("ERROR: ".$str);
}

回答by duskwuff -inactive-

It doesn't have to be die()specifically, but it needs to be something that'll make the script halt by calling exit()or die(), or something that throws an exception. Otherwise, the script will continue with the return value of that function (which is probably either null or some sort of junk) in $update_result, which will almost certainly cause problems.

它不必是die()特定的,但它必须是通过调用exit()或使脚本停止的die()东西,或者是引发异常的东西。否则,脚本将继续使用该函数的返回值(可能是 null 或某种垃圾) in $update_result,这几乎肯定会导致问题。