0

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

enter image description here

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.

Sachin
  • 1,646
  • 3
  • 22
  • 59

1 Answers1

-2

A simple approach to hide the warning message produced by PHP and to show only your own custom message is to use suppress operator (@).

Change line from

$mysqli = new mysqli($hostname, $username, $password, $dbname);

to

@$mysqli = new mysqli($hostname, $username, $password, $dbname);
Sachin
  • 1,646
  • 3
  • 22
  • 59
  • the only problem, there should be not a single die() statement – Your Common Sense Jul 30 '20 at 12:58
  • @YourCommonSense This was just a test file for making and testing db connection. It has nothing to do with a project. I don't understand why is this solution marked negative. – Sachin Jul 30 '20 at 14:25
  • Because this solution is wrong and makes a bad example for the future readers. Hence naturally it is marked so. – Your Common Sense Jul 30 '20 at 14:58
  • Please note this solution is wrong for a test file as well. your files shouldn't really differ. A good code is good for either test or production. – Your Common Sense Jul 30 '20 at 14:59
  • @YourCommonSense Okay but I have read in several IT books written by famous authors that to suppress errors generated by MySQL or other functions, you should use @ operator. Are all they wrong? – Sachin Jul 30 '20 at 15:13
  • Yes. Sadly, these authors are more famous than knowledgeable and their books are quite outdated. I don't really understand why did you dismiss the display_errors recommendation? It prevents all errors from displaying at once. Really handy. Not to mention the moment you need them displayed (on a test server) you just change one digit per whole project. Why going around adding @s and die()s when PHP can do it for you? – Your Common Sense Jul 30 '20 at 15:17
  • @YourCommonSense Oh! your statement is really very shocking. So you mean the authors of big publishers like Oreilly, Apress and Wrox, etc. are not that knowledgeable and skillful, right? If so then why they have been hired by these publishers? And not other guys like me, you and others? Also, you said their books are outdated but I'm not talking about their very old versions - they publish new editions time to time. And you know very well that the thing which is relevant today could easily become outdated in near future. You might have read their 90s or early 2Ks versions. – Sachin Jul 30 '20 at 15:27
  • @YourCommonSense Besides these things, I know the stuff like `display_errors` etc. But my quest was different at that time. – Sachin Jul 30 '20 at 15:29
  • Actually, such "new" editions are rather "facelifting" than real improvements. The notorious example is a Welling/Thomson book that has outdated/dangerous code all around. – Your Common Sense Jul 30 '20 at 15:30
  • Nobody question your knowledge. I am just curious why this function doesn't suit you in this particular case? It does solve your problem, in a much better way than @. – Your Common Sense Jul 30 '20 at 15:31