How can we stop PHP MySQLi from displaying ugly warning or error messages if the database connection is failed? We just want to display our own custom message instead.
Here's our code to make connection with db:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$mysqli = new mysqli($hostname, $username, $password, $dbname);
if($mysqli->connect_error)
{
die("Database connection failed: " . $mysqli->connect_error);
}
$mysqli->close();
?>
If the database "db_test" doesn't exist, we get the following error:
( ! ) Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'db_test' in...
We just want to see this:
Database connection failed: Unknown database 'db_test'
What editing do I need to do in my above code?
Note
I know the database db_test doesn't exist. I am deliberately using wrong db just in order to see the warning or error message if any. I just want to see my custom error message if db connection failed (if it also fails in real scenario by any reason too) and not the warning generated from PHP.
EDIT
This warning message is only produced when I use OOP approach in MySQLi, and not with procedural.