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
It must be because you installed PHP >= 8.1
I suggest you rewrite your code with try-catch statements:
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.
In new versions of
PHP
, when amysqli
connection fails, aFatal 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 aFatal Error
. the porblem should be solved if you usetry-catch
blocks instead of checkingmysqli_connect_errorno()
function.try to run this script instead:
More details about
try-catch
structureIMPORTANT 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.