0
while($rowForStateList  =   @mysql_fetch_array($resForStateList))

{
    $sid[$i]    =   $rowForStateList['state_auto_id'];

    $sname[$i]  =   $rowForStateList['state_name'];

    $spid[$i]   =   $rowForStateList['country_auto_id'];

    $i++;
}

AND

while($rowForStateList  =   mysql_fetch_array($resForStateList))

{
    $sid[$i]    =   $rowForStateList['state_auto_id'];

    $sname[$i]  =   $rowForStateList['state_name'];

    $spid[$i]   =   $rowForStateList['country_auto_id'];

    $i++;
}
MrRap
  • 137
  • 13
user909058
  • 188
  • 1
  • 2
  • 11
  • The `@` sign is the difference. There is a question: What does this symbol means here on SO. – hakre Aug 25 '11 at 08:23
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – hakre Aug 25 '11 at 08:24

12 Answers12

2

The difference is that @mysql_fetch_array in first code sample fetches the array suppressing any errors, while mysql_fetch_array do the same but not suppressing errors. Actually, the second one is more correct way.

The evil of using @ is in that it comlicates debugging a lot. With this suppression, in case of error you'll end up with empty arrays, so it will look just like query used to get $resForStateList have returned empty result. But, you actually might have broken query, refused database connection and whatever else. And with @ you will never know that something goes wrong.

So, don't use @. Instead, use error handling functions. And the best way is to check if something could cause error, e.g. mysql_query returns false in case of any error, so you might want to check it like

$result = nysql_query("qwerty");
if (!$result){
    echo mysql_error();
}
J0HN
  • 26,063
  • 5
  • 54
  • 85
  • +1 for saying that using @ is incorrect. You might want to add why though. – GordonM Aug 25 '11 at 08:27
  • So, would somebody explain why this post is downvoted? Am I wrong? :) – J0HN Aug 25 '11 at 08:42
  • @JOHN, you got me. Mine got downvoted too, I think someone got their panties in a twist over posts that said or implied that the @ operator wasn't a good idea. – GordonM Aug 25 '11 at 14:15
1

The difference is that the former will mask any errors that occur in mysql_fetch_array(), making debugging more difficult.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

The @ in the first version basically means "If this function triggers an error, just hide it, don't log or display it".

Don't ever use the @ error suppression operator! Not only does it make for a debugging nightmare, but it also harms application performance. Use error_reporting, log_errors and display_errors to set proper error message behaviour instead.

GordonM
  • 31,179
  • 15
  • 87
  • 129
1

Don't use the @ operator in this case. mysql_fetch_array will only throw an error if the resource is not valid. You should have checked this before, after you got this resource.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

The difference is the error reporting. If you set a @ before mysql_fetch_array, any errors will be ignored. see php errorcontrol

Georg Leber
  • 3,470
  • 5
  • 40
  • 63
0

the only difference is that the @ in front of the mysql_fetch_array in the first code will supress any error from being displayed on the screen.

Christophe
  • 4,798
  • 5
  • 41
  • 83
0

the first one suppresses errors: http://davidwalsh.name/suppress-php-errors-warnings

Flask
  • 4,966
  • 1
  • 20
  • 39
0

They are identical. The first mysql_fetch_array() will not produce error messages due to the suppression operator @.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
0

The first one will silently ignore any error. The second one will display any error or exceptions thrown the mysql_fetch_array (for example, connection problem). The magic comes from the @ sign.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
0

@mysql_fetch_array($resForStateList)) doesn't show up any message in case of an error. That's all.

HBublitz
  • 670
  • 4
  • 6
0

The @ is an operator thet suppresses errors. Your statement will not throw errors when the @ is added. You can read more here: http://thesmithfam.org/blog/2006/05/07/php-the-operator/

0

When you prepend '@' to an expression, any error messages that might be generated will be ignored (ie. no error output will be generated). Anyway if the track_errors feature is enabled, any error message generated by the expression will be saved (and overwritten on each error) in the variable $php_errormsg.

Check PHP Error Control for more details.

Actually, I prefer to use expression widthout '@' and handle all exceptions with a function that log all message in a file with error_log(). Something like:

function error_handler($errno, $errstr, $errfile=null, $errline=null, $errcontext=null) {
    error_log($errstr, 3, 'debug.log');
}

set_error_handler('error_handler', ERROR_REPORTING);
jbrond
  • 727
  • 8
  • 20