简体   繁体   中英

php mysql_connect Warning disable

I have php script wich 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 faild the first time. Is there any way to disable the warning message only for mysql_connect() function?

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.

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.

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.

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.

I put mysql queries in try syntax to avoid displaying messages

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM