472,111 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

MySQL PHP =@ symbol

I've seen this in php.net $r=@mysql_query($query,$MySQL);
and I do not understand what would be the difference of this :
$r=mysql_query($query,$MySQL);

I mean what is the purpose of " =@ " ?
Does anyone know ? I am Just curious
Thanks

Jul 17 '05 #1
16 28605
Angelos wrote:
I've seen this in php.net $r=@mysql_query($query,$MySQL);
and I do not understand what would be the difference of this :
$r=mysql_query($query,$MySQL);

I mean what is the purpose of " =@ " ?
Does anyone know ? I am Just curious


It prevents errors from being displayed when the function is called.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #2
>> I've seen this in php.net $r=@mysql_query($query,$MySQL);
and I do not understand what would be the difference of this :
$r=mysql_query($query,$MySQL);

I mean what is the purpose of " =@ " ?
Does anyone know ? I am Just curious


It prevents errors from being displayed when the function is called.


That sounds usefull... but I thought that if error reporting is OFF then
errors are not displayed but then again you propably mean that it doesn't
display MySQL errors...
;-)
Jul 17 '05 #3
Angelos wrote:
I've seen this in php.net $r=@mysql_query($query,$MySQL);
and I do not understand what would be the difference of this :
$r=mysql_query($query,$MySQL);

I mean what is the purpose of " =@ " ?
Does anyone know ? I am Just curious


It prevents errors from being displayed when the function is called.


That sounds usefull... but I thought that if error reporting is OFF
then errors are not displayed but then again you propably mean that it
doesn't display MySQL errors...


If error reporting is set to *show* errors, then the @ symbol before a
function call will prevent the error from displaying. Obviously it
won't show if error reporting is set to not show them ;)

MySQL errors aren't displayed anyway, unless you make a syntax error
when calling the function. To find out if MySQL returned an error you
need to use the mysql_error() or mysql_errno() functions.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #4
> MySQL errors aren't displayed anyway, unless you make a syntax error
when calling the function. To find out if MySQL returned an error you
need to use the mysql_error() or mysql_errno() functions.


oh ok, didn't know that...
I always use mysql_error() anyway...
Jul 17 '05 #5
"Angelos" <an*****@redcatmedia.net> wrote in news:d6ck7q$fo7$1
@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
MySQL errors aren't displayed anyway, unless you make a syntax error
when calling the function. To find out if MySQL returned an error you
need to use the mysql_error() or mysql_errno() functions.


oh ok, didn't know that...
I always use mysql_error() anyway...


the '@' is most valuable when you're requesting variables that have been
POSTed or REQUESTed:

@$Name = $_POST['Name'];

So, in case someone DIDN'T fill out the field 'Name', the script won't go
all ugly. It lets you handle errors gracefully. Without the @ in the
above line, the output would be "Notice: Undefined Variable 'Name'" or
something. Instead I can just add:

if ($vName=="") {
echo "Sorry, please fill out your name.";
exit;
}

.... to handle the error gracefully...
Jul 17 '05 #6
Good Man wrote:
"Angelos" <an*****@redcatmedia.net> wrote in news:d6ck7q$fo7$1
@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
MySQL errors aren't displayed anyway, unless you make a syntax error
when calling the function. To find out if MySQL returned an error
you need to use the mysql_error() or mysql_errno() functions.


oh ok, didn't know that...
I always use mysql_error() anyway...


the '@' is most valuable when you're requesting variables that have
been POSTed or REQUESTed:

@$Name = $_POST['Name'];

So, in case someone DIDN'T fill out the field 'Name', the script won't
go
all ugly. It lets you handle errors gracefully. Without the @ in the
above line, the output would be "Notice: Undefined Variable 'Name'"
or
something. Instead I can just add:

if ($vName=="") {
echo "Sorry, please fill out your name.";
exit;
}

... to handle the error gracefully...


You could do it like this instead of using the @, although your way is
less verbose.

$name = isset($_POST['name']) ? $_POST['name'] : '';

An advantage of doing it this way is it this way lets you specify a
default value.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #7
Chris,
You could do it like this instead of using the @, although your way is
less verbose.

$name = isset($_POST['name']) ? $_POST['name'] : '';

An advantage of doing it this way is it this way lets you specify a
default value.


sometimes an even better way is with empty, for say if a field is
required data :)

if (empty($_POST['name'])) {
echo('Name must be filled out.');
}
Jul 17 '05 #8
if ($vName=="") { also produces E_NOTICE errors, so, don't do this.
Use isset() or empty(). Also for what it's worth, the @ error operator
is documented in the PHP Manual here:

http://php.net/manual/en/language.op...rorcontrol.php

Jul 17 '05 #9
Mike Willbanks wrote:
Chris,
You could do it like this instead of using the @, although your way
is less verbose.

$name = isset($_POST['name']) ? $_POST['name'] : '';

An advantage of doing it this way is it this way lets you specify a
default value.


sometimes an even better way is with empty, for say if a field is
required data :)

if (empty($_POST['name'])) {
echo('Name must be filled out.');
}


That's cool. Isn't it fun how you can program in a language for 7 years
and still not know a little thing like that :)

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #10
That's a common misconception, equating error messages with errors.
E_NOTICE and E_WARNING messages do not necessarily indicate errors.
Their purpose is to get a programmer to take a second look at some code
that could potentially be erroreous.

PHP emits a E_NOTICE or E_WARNING message whenever it fails to do
something (e.g. a hash table miss). Failures are not the same as
errors, one has to remember. When they're expected and are handled,
then there's nothing errorous in the code.

I don't understand why people insist that the use of isset() is good
practice. In order to see if an element is available in a hash table,
PHP has to look it. So you end up doing two hash lookups to get one
element.

Jul 17 '05 #11
Chung Leong wrote:
That's a common misconception, equating error messages with errors.
E_NOTICE and E_WARNING messages do not necessarily indicate errors.
Their purpose is to get a programmer to take a second look at some
code that could potentially be erroreous.

PHP emits a E_NOTICE or E_WARNING message whenever it fails to do
something (e.g. a hash table miss). Failures are not the same as
errors, one has to remember. When they're expected and are handled,
then there's nothing errorous in the code.

I don't understand why people insist that the use of isset() is good
practice. In order to see if an element is available in a hash table,
PHP has to look it. So you end up doing two hash lookups to get one
element.


I develop with the error reporting level to show notices so that I can
ensure I don't miss using a variable that hasn't yet been assigned (and
which certainly helps speed up debugging stupid coding issues).

If isset() is inefficient for the reason you have stated, then what
would you recommend as a more efficient method, given that you can
never assume a value has actually been passed to a page in a get or
post?

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #12
On 2005-05-17, Chris Hope <bl*******@electrictoolbox.com> wrote:
Mike Willbanks wrote:
Chris,
You could do it like this instead of using the @, although your way
is less verbose.

$name = isset($_POST['name']) ? $_POST['name'] : '';

An advantage of doing it this way is it this way lets you specify a
default value.


sometimes an even better way is with empty, for say if a field is
required data :)

if (empty($_POST['name'])) {
echo('Name must be filled out.');
}


That's cool. Isn't it fun how you can program in a language for 7 years
and still not know a little thing like that :)


Don't know which one is fastest:

- isset
- empty
- array_key_exists

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #13
Tim Van Wassenhove wrote:
On 2005-05-17, Chris Hope <bl*******@electrictoolbox.com> wrote:
Mike Willbanks wrote:
Chris,
You could do it like this instead of using the @, although your way
is less verbose.

$name = isset($_POST['name']) ? $_POST['name'] : '';

An advantage of doing it this way is it this way lets you specify a
default value.

sometimes an even better way is with empty, for say if a field is
required data :)

if (empty($_POST['name'])) {
echo('Name must be filled out.');
}


That's cool. Isn't it fun how you can program in a language for 7
years and still not know a little thing like that :)


Don't know which one is fastest:

- isset
- empty
- array_key_exists


They're all essentially doing the same thing so they probably take about
the same amount of time.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #14
Well, getting back to the OP's question, that's why we have the @
operator. If a message is bogus, just ignore it.

The method posted by Good Man above is what I use. You suppress the
error message from the hash access, then check the variable for
content:

$name = @$_POST['name'];
if(!$name) { ... }

More likely I would do:

$name = trim(@$_POST['name']);
if(!$name) { ... }

To distinguish between empty string and undefined variable:

$something = @$_GET['something'];
if(is_null($something)) { ... }

I mean that's how you'd access a hash table in C/C++: perform the look
up and deal with potential null results.

Jul 17 '05 #15
E_NOTICE is an error. E_ERROR is an error. E_PARSE is an error. These
are all errors of different levels, there is no misconception here.
Fact is you are promoting bad practice which is your right. Ask the
php.internals list and 100% of the people polled will tell you to use
isset() instead of @ here.

Jul 17 '05 #16
If you have an argument to make, make it.

Jul 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dennis Francis B. Tutanes | last post: by
reply views Thread by al | last post: by
reply views Thread by Greg O. | last post: by
1 post views Thread by grabro | last post: by
1 post views Thread by edfialk | last post: by
5 posts views Thread by Jagadish Kumar, Maripi | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.