Reputation: 637
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?
Upvotes: 13
Views: 32111
Reputation: 1447
I put mysql queries in try syntax to avoid displaying messages
try {
some code of sql
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
Upvotes: 0
Reputation: 11853
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
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.
Upvotes: 4
Reputation: 3280
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.
Upvotes: 16
Reputation: 64526
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.
Upvotes: 2
Reputation: 2335
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.
Upvotes: -1