Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mysql_connect Warning disable

Tags:

php

mysql

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?

like image 285
Gayane Avatar asked Jun 04 '13 08:06

Gayane


1 Answers

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.

like image 60
Software Guy Avatar answered Oct 01 '22 08:10

Software Guy