Should we check mysqli_connect() errors manually?
P粉031492081
P粉031492081 2023-10-27 10:51:49
0
1
491
The PHP manual for

mysqli_connect() recommends checking the return value and displaying an error message on the screen.

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

Similarly, for OOP-style constructors, this is recommended:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

Some users on Stack Overflow have even used code with mysqli_error($conn) like this:

$conn = mysqli_connect('localhost', 'a', 'a');
if (!$con) {
    die('Could not connect: ' . mysqli_error($conn));
}

But, over the past few weeks, I've been asking myself the question, why do I need to do this? The output of the first example is:

Warning: mysqli_connect(): (HY000/1045): User access denied 'my_user'@'localhost' (use password: YES) C:xampp...mysqli.php line 4

Error: Unable to connect to MySQL. Debugging error number: 1045 Debugging Error: Access denied for user "my_user"@"localhost" (using password: Yes)

As you can see, the error message is displayed twice! Manual "debugging" actually provides less information.

Should we manually check for connection errors? Can we get more information than automated warnings this way? Is this recommended practice?

P粉031492081
P粉031492081

reply all(1)
P粉251903163

Never display connection errors manually!

MySQLi will generate a warning if it cannot open a connection to MySQL. This warning tells you everything you need to know, including the error code, error message, and where in the code the error occurred. Manually checking for errors won't give you any more information.

If you don't see warnings and can't create a connection, it probably means your PHP is not configured to display them. In this case, you must check the error log file on the server. If you don't know where it is, use phpinfo() code> to get the information and search for error_log. It will tell you where the error log files are located.

If there are no warnings in the error log, it probably means that your PHP error reporting has been silenced (completely or just warnings). Check your PHP configuration.
In a production environment these settings should be preserved:

  • error_reporting must be E_ALL
  • log_errors Mustenable
  • display_errors MustClose

In a development environment, these settings should be preserved:

  • error_reporting must be E_ALL
  • log_errors Mustenable
  • display_errors Mustenable

As you can see in the error message, your database username and password have been leaked to the end user. This is sensitive information that you don't want to show to anyone. In fact, ordinary users will not understand this mysterious message. This is why display_errors must always be turned off in a production environment. It is safe to log errors on the server.

Warnings and exceptions

Warnings do not stop the script. If a warning is issued, the script will continue execution until a fatal error is encountered. In most cases, you need to throw an exception to stop the script. Don’t use die/exit! If the mysqli connection cannot be established, an exception should be thrown, which if unhandled will bubble up and stop the script with a fatal error. You can configure mysqli to automatically throw exceptions. This is invaluable because all mysqli functions can fail for a variety of reasons, and they won't notify you of any problems unless you manually check for each error in them. Use the following lines before opening the connection:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

Don't catch exceptions unless you really know how to handle them! How to use mysqli to connect correctly

A possible use case is described in

mysqli_error() Can you show any connection related issues?

No. mysqli_error($conn) The mysqli connection is expected to be successful. $conn must be a valid mysqli connection, otherwise you will receive this error message:

Neither

$conn->error nor mysqli_error($conn) can display any connection-related errors!


Related: Should I manually check for errors when calling "mysqli_stmt_prepare"? p>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!