3

I am using simple method to connect to my database:

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Unable to connect to MySQL");
$conn->query("SET NAMES utf8") or die("Unable to connect to MySQL");

And its working fine, but when I directly typing wrong $servername (or else) then i got WARNING message:-

Warning: mysqli_connect(): (28000/1045): Access denied for user 'root'@'192.168.0.157' (using password: YES) in C:\MAMP\htdocs\modul-1-froland\zaro_\admin\db_config.php on line 7

And then die message:

Unable to connect to MySQL

There is any solution to hide WARNING message, without @ and error_reporting(E_ALL)? I want to 'user friendly' error messages (like: Unable to connect to MySQL) and not the all error message (like mysqli_connect()...).

4
  • Didn't understood why not to use "error_reporting"? Commented Jan 31, 2017 at 9:19
  • 2
    hide warning :- error_reporting(E_ERROR | E_PARSE); Commented Jan 31, 2017 at 9:19
  • cause i think, there is another way to hide warning messages, but seems like no :( Commented Jan 31, 2017 at 9:22
  • 2
    Possible duplicate of Remove warning messages in PHP Commented Jan 31, 2017 at 9:23

2 Answers 2

2

To hide warning messages:-

error_reporting(E_ERROR | E_PARSE);

Different other settings are clearly described here:- https://stackoverflow.com/a/2867082/4248328

Note:- personally I like to debug and found all errors and try to rectify them instead of hiding them (even if they are warnings or notices).Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

yes, me too, thats why i dont want use use error_report() and hide them, but thanks !
1

Remove the or die from the statement, which will stop the error message you are seeing.

Then if you want more friendly errors have a look at overriding the default php errors/exceptions.

User custom error handling set_error_handler and exception handler set_exception_handler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.