user5780640
user5780640

Reputation:

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

Upvotes: 3

Views: 836

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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

Upvotes: 2

RyanS
RyanS

Reputation: 78

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

Upvotes: 1

Related Questions