12

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Gayane
  • 627
  • 1
  • 11
  • 23

5 Answers5

15

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Software Guy
  • 3,190
  • 4
  • 21
  • 21
  • 4
    *sidenote:* stop using deprecated `mysql_*` functions. use MySQLi or PDO instead. using `@` is to suppress, not to handle. Use Try-Catch – Raptor Jun 04 '13 at 08:21
  • 4
    @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 04 '13 at 08:30
  • @Software You *should* really get rid of `$RC` and check for `$dblink` though. – deceze Jun 04 '13 at 08:32
  • @deceze even when you are expecting error you could program good solution instead of suppress that - mysql_connection returns resource or false in case of error – Ochi Jun 04 '13 at 08:34
  • @Ochi So what's your solution to *try to connect to a database without letting an error message print to screen?!* Post your own answer if you have a better one. – deceze Jun 04 '13 at 08:35
  • @deceze Ok, I should have said `Try to avoid using @`. But when newbies learn about `@` they tend to overuse it (for example they would even use it in `@$foo = $_GET['bar'];`). [Side image](http://i.stack.imgur.com/6a4ud.jpg) – HamZa Jun 04 '13 at 08:40
  • 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. – Software Guy Jun 04 '13 at 08: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 04 '13 at 08:47
  • Well, obviously in example code answers, that operator is superfluous code and should be removed. The only reason one might want to not log those errors is a system "designed for failure" (or even better worded: using a non-working expected database per-se). However this makes no sense as the code then uses a die statement. So actually this answer is just crap code. I now tried to improve it a little, no idea if it better tastes for all. – hakre Jun 04 '13 at 09:03
  • @deceze ignoring the discussion about `@`, your point about *without letting an error message print to screen* should be covered by `display_errors`, not by suppressing the error. – cmbuckley Jun 04 '13 at 21:40
  • @cbuckley display_errors setting will affect all errors in the page. The @ sign is useful if you want to hide only a specific error message / warning. – Software Guy Jun 05 '13 at 07:38
  • @SoftwareGuy error messages should never be printed to the screen anyway, except possibly in development. `@` not only hides from the screen, but hides from error logs as well (i.e. it fully suppresses it, not just causes it not to be displayed). – cmbuckley Jun 05 '13 at 08:27
  • I came to this post after having the same issue and already trying with Try-Catch. @ was the only way I found to stop the warning messages and be able to silently handle the situation. – ChrCury78 Mar 06 '20 at 19:06
5

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
liyakat
  • 11,825
  • 2
  • 40
  • 46
  • they just need to hide warning so we can also enable after just understand before you vote down @Ochi – liyakat Jun 04 '13 at 08:31
  • no - what You are proposing will ends with EVERY OTHER errors being suppressed - and that could lead to more serious problems – Ochi Jun 04 '13 at 08:33
  • or one more way we can also log in to the one text file to hide it from other. – liyakat Jun 04 '13 at 08:33
3

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.

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • 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 04 '13 at 08: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 04 '13 at 08:57
0

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.

HamZa
  • 14,671
  • 11
  • 54
  • 75
Benz
  • 2,317
  • 12
  • 19
0

I put mysql queries in try syntax to avoid displaying messages

try {
    some code of sql
} catch (Exception $e) {
    $errors[] = $e->getMessage();
}
Mahdi Shad
  • 1,427
  • 1
  • 14
  • 23