简体   繁体   English

@mysql_connect和mysql_connect

[英]@mysql_connect and mysql_connect

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_connect and mysql_connect ? @mysql_connectmysql_connect什么区别?

I have never used the @ symbol when writing my own script so was just wondering if it served a purpose. 我在编写自己的脚本时从未使用过@符号,所以只是想知道它是否有用。

Thanks in advance 提前致谢

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 depreciate that function. 另外,正如@AlexanderLarikov所说的那样,不要再使用mysql_*了,社区已经开始贬低该功能。

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. 除其他外,这意味着如果您使用“ @”抑制某个功能的错误,并且该功能不可用或输入错误,该脚本将在那里死掉,而没有任何指示。

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. 我个人认为使用此方法是不正确的做法,因为我认为您应该编写代码来处理错误,而不仅仅是默默地丢弃它们。

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 ...功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM