0

Please, can I know what's wrong with this function. I want if the connection to database is not established to display a custom error message instead of the default exeption.

<?php 
    $host = "localhost";
    $user = "root";
    $pass = "";
    $dbname = "ts";

    $dbc = mysqli_connect($host,$user,$pass,$dbname);

    if( !$dbc){
        echo "failed to connect with database";
    }
?>

The right name of database is: tst Please how to solve the problem?

  • 2
    Nope you don't. The custom message "failed to connect with database" makes absolutely no sense. – Your Common Sense Jun 23 '22 at 10:06
  • 2
    The only custom error message you want is "An internal server error has been occurred, please try again later." And how to get it you can see [here](https://phpdelusions.net/articles/error_reporting) – Your Common Sense Jun 23 '22 at 10:09
  • BTW, never configure your web app to login to the database as `root`. Root can do whatever it likes, so if you accidentally have any vulnerabilities this just leaves your database an open book for hackers. Instead create a separate user account specifically for this application which has only the permissions it actually _needs_ in order to work properly. Don't even use the root account as a shortcut during development or testing, because you need to test your account permissions as well - otherwise when you go live you might have unexpected errors relating to the user account setup. – ADyson Jun 23 '22 at 10:44

0 Answers0