avoid error logging with mysql connection

Asked

Viewed 145 times

1

Guys I make a connection to mysql using php. I do so:

mysqli_connect($ip_bd_mysql, $login_bd_mysql, $senha_bd_mysql, $banco);

The problem is that it is time that the name of the BANK is wrong, and the system returns me an error. How do I hide this error and customize it. to inform that there is an error with the connection data.

  • Do you display errors in production environment? an if in connection already solves.

  • in production environment no. But it keeps generating a log with the error.

2 answers

2


You can use a trycatch for any exception that occurs it falls into an area where you can customize the response:

try {
    mysqli_connect($ip_bd_mysql, $login_bd_mysql, $senha_bd_mysql, $banco);
} catch (Exception $e) {
    return "Minha mensagem...";
}

You can still force mistakes, and it will fall into this same catch, for example:

try {
    if($ip_bd_mysql == '127.0.0.1')
          throw new Exception("Minha mensagem de erro customizada");
    mysqli_connect($ip_bd_mysql, $login_bd_mysql, $senha_bd_mysql, $banco);
} catch (Exception $e) {
    return $e->getMessage();
}

In the variable $e, you still have methods to have relevant error information such as original error message, file and line that error occurred among others...

To see exactly just give one var_dump(get_class_methods($e));.

To disable warning and notice try to put the following snippet before the execution:

error_reporting(0);
  • I tried that way and the error continues

  • Which error in the log exactly it is generating, in this case it will catch only FATAL_ERROR, if it is a NOTICE or WARNING, ai you will need to disable in production so that do not record these logs...

  • I edited the answer.

1

Good morning!

If you want to ignore only the error of this function, try using the @ together:

@mysqli_connect($ip_bd_mysql, $login_bd_mysql, $senha_bd_mysql, $banco);

Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.