@mysql_connect and mysql_connect

白昼怎懂夜的黑 提交于 2019-12-31 13:30:51

问题


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.

What is the difference between @mysql_connect and 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


回答1:


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.




回答2:


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.




回答3:


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).

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.




回答4:


The @ supresses warnings http://php.net/manual/en/language.operators.errorcontrol.php use it wisely




回答5:


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.



来源:https://stackoverflow.com/questions/11742824/mysql-connect-and-mysql-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!