Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to database without writing warning/error messages

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


1 Answers

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

like image 157
Anant Kumar Singh Avatar answered Feb 03 '26 06:02

Anant Kumar Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!