I cant figure out the diffrence between following two syntaxes
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_fet开发者_开发知识库ch_array($resForStateList))
{
$sid[$i] = $rowForStateList['state_auto_id'];
$sname[$i] = $rowForStateList['state_name'];
$spid[$i] = $rowForStateList['country_auto_id'];
$i++;
}
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();
}
The difference is that the former will mask any errors that occur in mysql_fetch_array()
, making debugging more difficult.
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.
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.
The difference is the error reporting. If you set a @ before mysql_fetch_array, any errors will be ignored. see php errorcontrol
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.
the first one suppresses errors: http://davidwalsh.name/suppress-php-errors-warnings
They are identical. The first mysql_fetch_array()
will not produce error messages due to the suppression operator @
.
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.
@mysql_fetch_array($resForStateList))
doesn't show up any message in case of an error.
That's all.
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/
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);
精彩评论