skip to Main Content

My current computer has XAMPP installed and I have used it successfully to test PHP programs for some years. I recently bought a new computer and have installed the latest version of XAMPP on it, as well as transferring a number of PHP application programs to it. When trying to connect to a database, I use the following logic:

<?php
function dbConnect() {`
@ $db = new mysqli('localhost', 'centr120_genuser', 'Baroque73!', 'centr120_cmp' );
$test = mysqli_connect_errno();
  if (mysqli_connect_errno()) {
    @ $db = new mysqli('localhost', 'pract458_genuser', 'Gunjur64!', 'pract458_cmp' );
    if (mysqli_connect_errno()) {
      $test = mysqli_connect_errno();
      echo "Error # ".$test."<br/>";
      echo 'Error: Could not connect to database. Please try again later.';
      exit;
    }
  }
return $db;
}
?>

This logic is used by all my applications on a number of different host servers to connect to a database, and it always works fine. First, a connection to the client’s live database is attempted. If this fails because that database is not available or some other error occurs, then an attempt to connect to the test database is made.

On the new computer, on executing the first connection attempt, I get the message:
Fatal error: Uncaught mysqli_sql_exception: Access denied for user ‘centr120_genuser’@’localhost’ (using password: YES) in C:xampphtdocscmpconnectdb.php:3 Stack trace: #0 C:xampphtdocscmpconnectdb.php(3): mysqli->__construct(‘localhost’, ‘centr120_genuse…’, Object(SensitiveParameterValue), ‘centr120_cmp’) #1 C:xampphtdocscmpindex.php(17): dbConnect() #2 {main} thrown in C:xampphtdocscmpconnectdb.php on line 3

For some reason, a fatal error occurs on the first connection attempt, which means that the test for the error number ( mysqli_connect_errno() ) never gets executed.

Does anyone know why this happens?

2

Answers


  1. It must be because you installed PHP >= 8.1

    mysqli_report: As of PHP 8.1.0, the default setting is MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT. Previously, it was MYSQLI_REPORT_OFF.

    I suggest you rewrite your code with try-catch statements:

    function dbConnect() {
        try {
            return new mysqli('localhost', 'centr120_genuser', 'Baroque73!', 'centr120_cmp');
        } catch (mysqli_sql_exception) {}
    
        return new mysqli('localhost', 'pract458_genuser', 'Gunjur64!', 'pract458_cmp' );
    }
    

    This way, PHP will return the first connection if it works, and otherwise will try to create the second one.

    Note that printing something like "Error: Could not connect to database. Please try again later." is a bad practice. Your code should be able to handle any fatal error, not only the database connection. And the error message must be generic, without any particular details.

    Login or Signup to reply.
  2. In new versions of PHP, when a mysqli connection fails, a Fatal Error will be raised.

    This prevents running unexpected codes after failed queries after a failed connection; for example when an insert query has been executed, you might want to create a flag file or something else. If with any reason your DB connection fails, then how to sure that you have a plan to catch the error and don’t create that important flag file?

    because of this, PHP stops executing the script with raising a Fatal Error. the porblem should be solved if you use try-catch blocks instead of checking mysqli_connect_errorno() function.

    try to run this script instead:

    <?php
    function dbConnect() {
      $db = null;
      try {
        # first connection
        $db = new mysqli('localhost', 'centr120_genuser', 'Baroque73!', 'centr120_cmp');
      } catch(mysqli_sql_exception $ex) {
        try {
          # second connection
          $db = new mysqli('localhost', 'pract458_genuser', 'Gunjur64!', 'pract458_cmp');
        } catch(mysqli_sql_exception $ex2) {
          echo "Error # ".mysqli_connect_errno()."<br/>";
          echo 'Error: Could not connect to database. Please try again later.';
          exit;
        }
      }
      return $db;
    }
    ?>
    

    More details about try-catch structure

    IMPORTANT SECURITY WARNING:

    Please note that you just published your private passwords and now, you MUST to change them even you think nobody can access your virtual server or anything else.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search