13

I have php script which should try to connect to DB in local site. If the local DB is not available it should try to connect to DB on remote server.

$dblink = mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS) or $RC = 1;
if($RC) {
    $dblink = mysql_connect(DBHOST_REMOTE, DBUSER, DBPASS) or die('Could not connect'.mysql_error());
}

The problem is that I don't want to display Warning message on page if connection failed the first time. Is there any way to disable the warning message only for mysql_connect() function?

4
  • 7
    Don't use mysql_* as they're deprecated. Use PDO or mysqli_* instead Jun 4, 2013 at 8:19
  • 3
    You should really remove the or $RC = 1 part. Simply check for !$dblink. Jun 4, 2013 at 8:21
  • 1
    This could be handled much more elegantly by catching exceptions if you used mysqli or PDO, which you should anyway instead of the deprecated mysql API.
    – deceze
    Jun 4, 2013 at 8:38
  • @hammar I support the existing answer by Software Guy and have in fact edited it.
    – deceze
    Jun 4, 2013 at 11:47

5 Answers 5

16

Yes, add an @ sign like so to suppress warning / error messages, then do the error once your own:

$dblink = @mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS);

if (!$dblink) 
{
    $dblink = @mysql_connect(DBHOST_REMOTE, DBUSER, DBPASS);                  
}

if (!$dblink)
{
    $message = sprintf(
        "Could not connect to local or remote database: %s",
        mysql_error()
    );
    trigger_error($message);
    return;
}

Take care that you need to handle all error reporting your own then. Such code is hard to debug in case you make a mistake.

13
  • 4
    sidenote: stop using deprecated mysql_* functions. use MySQLi or PDO instead. using @ is to suppress, not to handle. Use Try-Catch
    – Raptor
    Jun 4, 2013 at 8:21
  • 5
    @HamZa @ is fine, if you are aware that you are suppressing errors and are handling them! In this case, he's checking whether the connection was successful or not, expecting an error. He's just not letting the error message print to screen. You should use @ as little as possible, but that doesn't mean to never use it when you know what you're doing.
    – deceze
    Jun 4, 2013 at 8:30
  • @Software You should really get rid of $RC and check for $dblink though.
    – deceze
    Jun 4, 2013 at 8:32
  • 5
    it always ends up being a debate when "Best Practice Lords" unleash their thunder on pragmatic solutions that work. Sir @HamZa the @ sign is provided in PHP for exactly the reason I shared it for. I know there are better ways to handle errors but this is not the discussion nor the question here. Jun 4, 2013 at 8:43
  • 3
    @ is an operator. It has no free will. It has no side of Evil or Good. Evil or Good comes from people, that use it: are they thinking about it, or are they not.
    – BlitZ
    Jun 4, 2013 at 8:47
4

you can set error reporting error_reporting(0); to turn off errors and warning for speacific page

just set as per your need

# config.ini 
# PHP error reporting. supported values are given below. 
# 0 - Turn off all error reporting 
# 1 - Running errors 
# 2 - Running errors + notices 
# 3 - All errors except notices and warnings 
# 4 - All errors except notices 
# 5 - All errors 

Doc

just set at head in page like this

<?php 

      error_reporting(E_ERROR | E_WARNING | E_PARSE);

?>

let me know if i can help you more.

3
  • they just need to hide warning so we can also enable after just understand before you vote down @Ochi
    – liyakat
    Jun 4, 2013 at 8:31
  • 1
    no - what You are proposing will ends with EVERY OTHER errors being suppressed - and that could lead to more serious problems
    – Ochi
    Jun 4, 2013 at 8:33
  • or one more way we can also log in to the one text file to hide it from other.
    – liyakat
    Jun 4, 2013 at 8:33
2

The proper solution is to disable the displaying of PHP errors, warnings and notices to the user. This can be done via the display_errors setting in your php.ini configuration file:

display_errors = Off

It can also be set at runtime via ini_set():

ini_set('display_errors', '0');

From the Manual:

display_errors
This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.

Doing that would prevent any users from seeing sensitive PHP error messages, but still allow them to be printed to the log file.

Use of the @ suppression method would do what you want but it would still allow any other PHP errors/warnings to be printed to the user, which is a security issue because those errors can contain sensitive information that should not be exposed.

2
  • 1
    Yes and no. You surely are right, but during development at least you do not want to suppress the display of unexpected errors. The OP wants to suppress one very specific expected error though...
    – deceze
    Jun 4, 2013 at 8:51
  • 1
    @deceze it's down to personal developer preference as to displaying errors during development. Relying on errors on the screen could lead to missing errors because depending on the structure of the page and the point that the message is printed, you may not actually see the error in the browser without doing View > Source.
    – MrCode
    Jun 4, 2013 at 8:57
0

I put mysql queries in try syntax to avoid displaying messages

try {
    some code of sql
} catch (Exception $e) {
    $errors[] = $e->getMessage();
}
-1

Can't you use the following method:

if (!$connection = mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS)) {
    die('And here we connect to the other server');
} 

When the connection fails, it returns a boolean, which will cause the if statement to be true.

1
  • 2
    That still prints an error to the screen though, exactly what the OP is trying to avoid.
    – deceze
    Jun 4, 2013 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.