php @mysql_connect 和 mysql_connect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11742824/
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
@mysql_connect and mysql_connect
提问by Lodder
I have no problem connecting to a database using PHP however in some scripts I have tested, I have seen a small difference in the connect command.
我使用 PHP 连接到数据库没有问题,但是在我测试过的一些脚本中,我发现 connect 命令存在细微差别。
What is the difference between @mysql_connectand mysql_connect?
@mysql_connect和 和有mysql_connect什么区别?
I have never used the @symbol when writing my own script so was just wondering if it served a purpose.
我@在编写自己的脚本时从未使用过这个符号,所以只是想知道它是否有用。
Thanks in advance
提前致谢
回答by Peon
The @symbol in front of a function silences it. Meaning, you won't get any types of error messages when executing it, even if it fails. So I suggest: don't use it
@函数前面的符号使其静音。这意味着,即使执行失败,您也不会在执行时收到任何类型的错误消息。所以我建议:不要使用它
In addition as @AlexanderLarikov said, don't use mysql_*anymore, the community has started to depreciatethat function.
此外,正如@AlexanderLarikov 所说,不要再使用mysql_*了,社区已经开始贬低该功能。
回答by JK.
It is the/an error control operator. It simply allow you to suppress the error.
它是/一个错误控制运算符。它只是允许您抑制错误。
I would suggest that you omit it in your code.
我建议您在代码中省略它。
From documentation:
从文档:
Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
目前,“@”错误控制运算符前缀甚至会禁用将终止脚本执行的严重错误的错误报告。除此之外,这意味着如果您使用“@”来抑制某个函数的错误,并且它不可用或输入错误,则脚本将立即死在那里,没有任何迹象表明原因。
回答by Mike Brant
That is an error suppression mechanism. So, say there was an an error when you tried to connect, PHP would silently ignore it rather than displaying/logging it (depending on your settings).
那是一种错误抑制机制。因此,如果您尝试连接时出现错误,PHP 会默默地忽略它而不是显示/记录它(取决于您的设置)。
I personally think it bad practice to use this, as, in my opinion, you should write your code to handle errors, not just silently discard them.
我个人认为使用它是不好的做法,因为在我看来,您应该编写代码来处理错误,而不仅仅是默默地丢弃它们。
回答by allen213
The @ supresses warnings http://php.net/manual/en/language.operators.errorcontrol.phpuse it wisely
@ supresses 警告http://php.net/manual/en/language.operators.errorcontrol.php明智地使用它
回答by K-Gun
If don't use any option something like this;
如果不使用任何类似这样的选项;
if ("I'm just making test on my srv") {
error_reporting(E_ALL);
} else {
error_reporting(0);
}
Then, it could be recommended for this situation;
然后,可以推荐用于这种情况;
$conn = @mysql_connect(...);
if ($conn === false) {
// handle error
}
Or;
@mysql_connect(...) or die("Could not connect to ...");
So, @suppresses the error if it exist at the line "where the suppressible function is used".
因此,@如果错误存在于“使用可抑制函数的位置”行,则抑制错误。
// Suppressible? Yes, cos you can not apply @to die, exit, eval... functions if these are structural functions.
// 可抑制?是的,因为你不能适用@于die, exit, eval...功能,如果这些结构的功能。

