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

Lodder
  • 19,758
  • 10
  • 59
  • 100
  • 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 Jul 31 '12 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 Jul 31 '12 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](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is good PDO tutorial](http://goo.gl/vFWnC). – Manse Jul 31 '12 at 14:53
  • if you need a PHP symbol reference try [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Patrick Jul 31 '12 at 14:53
  • @ManseUK: thanks for the info and references. – Lodder Jul 31 '12 at 15:03

5 Answers5

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.

Peon
  • 7,902
  • 7
  • 59
  • 100
  • 4
    in addition I can suggest: dont use `mysql_*` functions at all since it deprecated – Alexander Larikov Jul 31 '12 at 14:52
  • Also true, thanx for the comment here. – Peon Jul 31 '12 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) – Elias Van Ootegem Jul 31 '12 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 Jul 31 '12 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 Jul 31 '12 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.

JK.
  • 5,126
  • 1
  • 27
  • 26
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.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

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

allen213
  • 2,267
  • 2
  • 15
  • 21
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.

Kerem
  • 11,377
  • 5
  • 59
  • 58