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()...).

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

2 Answers2

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

Community
  • 1
  • 1
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    yes, me too, thats why i dont want use use error_report() and hide them, but thanks ! –  Jan 31 '17 at 09:30
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

RyanS
  • 78
  • 1
  • 10