• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

[PHP/MySQL] whats the @ symbol for?

Question

+unabatedshagie    10

Sorry for the topic title but I couldn't think what to put for it :D

Anyway, I have a couple of books on php/mysql and some of the examples have

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

while others will have

$db = mysql_connect($dbhost, $dbuser, $dbpassword);

so whats the difference between them?

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0
Esvandiary    0

If I remember correctly, the @ symbol stops any errors from that command coming out (I've used it with an fsockopen() where I wanted my own output instead of the script's default error message).

This is just my memory, though, so take it with a pinch of salt :rolleyes:

EDIT: Yeah, I'm pretty sure that's right now; just removed it from the line I had it in on my little utilities page and it comes out with a warning that it timed out. :)

Share this post


Link to post
Share on other sites
  • 0
deadpencil    0

yep, that's right - it suppresses error messages

other than that, there is no difference between your examples

Share this post


Link to post
Share on other sites
  • 0
Esvandiary    0

EDIT: Gah, you were too fast for me there ;)

Share this post


Link to post
Share on other sites
  • 0
+unabatedshagie    10

ok, thanks.

So I would be better off leaving them off when I'm developing the site then adding them back in later when everything works?

Share this post


Link to post
Share on other sites
  • 0
Belliash    0

Depends on what are You trying to do...

For example...

You try to connect to database and You probably would like to know if Your website was unable to do so.

If You don't use @ then it will just display an error message and probably die.

However You may just want to display sth like "DB connect failed".

To do so You write:

if!(mysql_connect(...)) {

die("DB connect fail");

}

not sure about example i written above, if it will work, but that's one of easiest way to show how does @ work...

Share this post


Link to post
Share on other sites
  • 0
+theblazingangel    137
ok, thanks.

So I would be better off leaving them off when I'm developing the site then adding them back in later when everything works?

not advisable! instead, stick this at the top of your script:

@ini_set('display_errors', 1);
error_reporting(E_ALL);

and then when your done developing, change it to:

ini_set('display_errors', 0);
error_reporting(0);

use @ sparingly!

Edited by theblazingangel

Share this post


Link to post
Share on other sites
  • 0
rpgfan    0

Using the @ operator is NOT advisable at all in a production environment. Why? Because it slows things down! If your server is getting hit by "The Digg Effect", your server will be slow for a while. In high-traffic cases such as that, you will want things to be as quick as possible, right? That is why things like this list were created. See tip #14 in that list. I personally would turn off display_errors, and turn on error logging. File I/O on the server side is often much faster than making a user wait for the browser only to see something like:

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/.sites/64/site14/web/forums/mods/flashChat/inc/classes/db.php on line 23

Of course, that was from a Linux machine (or possibly a machine running UNIX or a UNIX clone), but the point stands. That list is only a couple of months old, so I would definitely recommend it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.