I’ll try asking this again but without typing mistakes…. I’m connecting using MySQLi and using intentionally bad connection variables to trigger an error.
Working as expected:
$conn = mysqli_connect("localhost","x","x","x");
if (mysqli_connect_errno()) {
echo "Couldn't establish database connection blah blah";
}
This should work exactly the same, but it showing a “Can’t fetch MySQLi” warning:
$conn = new mysqli("localhost","x","x","x");
if ($conn->connect_errno) {
echo "Couldn't establish database connection blah blah";
}
Both methods are fine when the connection details are good, although that isn’t the point.
This didn’t work properly in some older versions of PHP.
If you are using a PHP version before 5.2.9, the
$conn->connect_error(and I think also$conn->connect_errno) method did not function properly, and you had to usemysqli_connect_errno()instead.The MySQLi constructor docs talk about this.