Solved

@mysql_query  and mysql_query whats the difference?

Posted on 2006-07-06
12
395 Views
Last Modified: 2013-12-12
hi

I have 2 scripts that do very similar things,  one has this line
  $ret = @mysql_query($sql);
and the other
  $ret = mysql_query($sql);

the SQL is for all puropses the same.  want to know what the @ mark means.  

michael
0
Comment
Question by:ussher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17049448
@ supresses that the page prints out a error message, so you can handle it (using mysql_error() )

@ can be used for ALL functions, not only mysql...
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 100 total points
ID: 17049466
@ means suppress any non fatal errors.

In fact it may even suppress them!

If using @, normally the next line of code will be determining if the result is a failure (in context with the function).

e.g. (from my own DB class)

      private function MakeDBConnection($objKrypt)
            {
            $resResult = @odbc_pconnect($this->DSN, $this->sUser, $objKrypt->Dekrypt($this->sPassword));
            if ($resResult === False)
                  {
                  throw new class_DB_Exception(
                        class_DB_Exception::DB_ERROR_SERVER_CONNECT,
                        array(
                              $this->sServer
                              )
                        );
                  }
            return $resResult;
            }

As you can see the odbc_pconnect() function can be false. Normally this will output a message also. By using @ the message is not reported and I can still continue testing the result.
0
 
LVL 1

Author Comment

by:ussher
ID: 17049470
so if error reporting is turned off at the server level both statements are exactly the same?
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17049487
I think so. But just because you can't see the error, doesn't mean the function hasn't worked!

Error suppression is not a route to take if you want to have a stable app.

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17049489
Too many negatives!

No error message does not mean the function HAS worked!
0
 
LVL 1

Author Comment

by:ussher
ID: 17049506
ok thanks.  for now i will remove it and see if there are any errors but once it gets to production i dont want clients seeing errors.

im a bit clearer at what im looking at now.

cheers
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17049527
No.

Wrong way round.

Keep it in and add a test to see if the returned value is the error value. In mysql_query()'s case that would be false.

$ret = @mysql_query($sql);
if (False === $ref)
 {
 // Then something went wrong. Depending upon the statement, you may be able to handle the error or not.
 }
else
 {
 // The query was executed successfully. I would now be checking the mysql_affected_rows() (for update/insert/delete) or mysql_num_rows() (for select) to make sure the query performed as I expected it to.
 }

0
 
LVL 1

Author Comment

by:ussher
ID: 17049570
ok thanks.  Ill get it printing something

if (False === $ref) why 3 equals signs?  I have seen == before and know what that menas but never 3.  does it have a different meaning?

michael
0
 
LVL 1

Author Comment

by:ussher
ID: 17049579
sorry. got it
 === identical operator

RTFM

thanks for your help

michael
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17049586
PHP is loosely typed.

This means that

0 == "0" == False

zero equals in value the string "0" equals in value False.

BUT === means equal in value AND type.

0 !== "0" !== False

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17049598
And the reason why you use False !== is that you want to ONLY test that the function returned a boolean True.

e.g.

strpos('ABC', 'A')

The answer is 0

But

<?php
if (False == strpos('ABC', 'A'))
 {
 echo 'A not present';
 }
?>

is clearly wrong.


<?php
if (False === strpos('ABC', 'A'))
 {
 echo 'A not present';
 }
?>

is the right way.

0
 
LVL 1

Author Comment

by:ussher
ID: 17049667
awesome.

im now one step closer to enlightenment. (had to get the manual out and looked up strpos)

thanks

michael

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question