10

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

5
  • 16
    @ is the php equivalent of VB's on error resume next e.g. "Who cares if the universe just exploded and everyone's dead. let's pretend everything's fine and continue shooting ourselves in the foot".
    – Marc B
    Commented Jul 31, 2012 at 14:52
  • If you want to surpress errors you should rather go the object oriented way and use try-catch mechanisms the OO versions of the functions :D
    – sinni800
    Commented Jul 31, 2012 at 14:53
  • 7
    Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial.
    – Manse
    Commented Jul 31, 2012 at 14:53
  • if you need a PHP symbol reference try Reference - What does this symbol mean in PHP?
    – Patrick
    Commented Jul 31, 2012 at 14:53
  • @ManseUK: thanks for the info and references.
    – Lodder
    Commented Jul 31, 2012 at 15:03

5 Answers 5

16

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.

5
  • 4
    in addition I can suggest: dont use mysql_* functions at all since it deprecated Commented Jul 31, 2012 at 14:52
  • Also true, thanx for the comment here.
    – Peon
    Commented Jul 31, 2012 at 14:53
  • well, don't use it is a bit harsh. I'd say: avoid, if possible. Say you're upgrading to php 5.4, and have set your ini to strict error and warning reporting, in that case you can use @ to suppress a couple of warnings and clean up your code gradually, instead of wading through all errors at the same time. In this case mysql_* will issue a deprecation warning, suppress them until you get round to sorted everything else out, and then switch them back on to see if you've gotten rid of all of them (replace them by PDO's) Commented Jul 31, 2012 at 14:54
  • @AlexanderLarikov: ahh crap, wish I had known that before, now have a lot of updates to make. thanks for the info ;)
    – Lodder
    Commented Jul 31, 2012 at 14:55
  • @DainisAbols: thanks for the answer. Was wondering why some of my scripts were not working and wasn't receiving any errors.
    – Lodder
    Commented Jul 31, 2012 at 14:56
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.

1

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.

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.