0

I'm trying to connect to the database using localhost.

<?php
     //Connecting to the Database
     $servername = "localhost";
     $username = "root";
     $password = "";

    // Create a connection
    $conn = mysqli_connect($servername, $username, $password);

    // Die if connection was not successful
    if (!$conn){
        die("Sorry we failed to connect: ". mysqli_connect_error());
    }
    else{
        echo "Connection was successful";
    }
?>

It works if the connection is made but I don't get the "Sorry we failed to connect" message when I intentionally create an error.

What could be the potential causes of this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Could you please add the code of mysqli_connect? – Jonas Metzler Apr 21 '22 at 07:22
  • What do you mean? It's there. I get this default error message: Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\phpp\dummydelete.php:8 Stack trace: #0 C:\xampp\htdocs\phpp\dummydelete.php(8): mysqli_connect('localhost', 'root', ' ') #1 {main} thrown in C:\xampp\htdocs\phpp\dummydelete.php on line 8. I want the "Sorry we failed to connect" error message. – Patrick2997 Apr 21 '22 at 07:26
  • You are using a method or function named "mysqli_connect" in this line: $conn = mysqli_connect($servername, $username, $password); Could you please add the source code of this function or method to your question so we can see it? – Jonas Metzler Apr 21 '22 at 07:29
  • It is an allias of mysqli::__construct(). https://www.php.net/manual/en/mysqli.construct.php – Patrick2997 Apr 21 '22 at 07:36
  • You are getting an error, why do you want to have a manual error too? – Dharman Apr 21 '22 at 09:35
  • 1
    @Patrick2997 i've got a really mind changing article for you: https://phpdelusions.net/articles/error_reporting following a few principles described there will let you inform both a programmer and a site user of a problem in the most appropriate form – Your Common Sense Apr 21 '22 at 10:34

1 Answers1

-2

As stated in the documentation:

If MYSQLI_REPORT_STRICT is enabled and the attempt to connect to the requested database fails, a mysqli_sql_exception is thrown.

Since you are getting an exception, this must be set. Your code should change as follows in this case:

try {
    $conn = mysqli_connect($servername, $username, $password);
} catch(mysqli_sql_exception $e) {
    die("Sorry we failed to connect: ". mysqli_connect_error());
}
echo "Connection was successful";
D B
  • 534
  • 3
  • 14
  • 1
    Echoing the error message right away is a very bad practice, for many reasons. Basically it assumes that the site will never go live, which is quite silly. Please read more here: https://phpdelusions.net/basic_principles_of_web_programming#error_reporting – Your Common Sense Apr 21 '22 at 09:40
  • @YourCommonSense You are right, but that was not the question at all. This is clearly code used in an early stage of development and therefore not in the scope of this question at all. – D B Apr 21 '22 at 09:46
  • Using two versions of code for the different stages is even more silly. I really cannot wrap my mind about such an approach: first we will add a useless code, just for sake of it, and then at some stage we will remove it. What gives? – Your Common Sense Apr 21 '22 at 10:36
  • @YourCommonSense Maybe I was not clear on what I meant - but you also try really hard to not understand. The code is literally the example from the documentation. If you think the code is awful and should not be used anywhere, you should go ahead and change the documentation. The expression "early stage of development" was meant to say: The first stage of development. – D B Apr 21 '22 at 13:04
  • Sorry? I don't see anything like that on the documentation page you linked to. – Your Common Sense Apr 21 '22 at 13:16
  • OPs code is directly from the documentation. My code is just an adjustment to make the exact same thing with the MYSQLI_REPORT_STRICT setting. – D B Apr 21 '22 at 13:47
  • That's too complex a logic for me, when "literally from documentation" becomes "is just an adjustment" :) Anyway, I just noticed that your "adjustment" makes no sense, doing only harm and nothing good or sensible. So you better rethink it. – Your Common Sense Apr 21 '22 at 13:54