Current Location: Home> Latest Articles> How to use mysqli::debug with error_log() to record custom logs to help debug MySQL connection problems?

How to use mysqli::debug with error_log() to record custom logs to help debug MySQL connection problems?

M66 2025-06-01

Debugging MySQL database connections is often a challenge when developing PHP applications. Especially in complex projects, it is important to track database connection errors, SQL execution issues, or other database-related exceptions. To help developers better debug, PHP provides mysqli extensions and some convenient logging tools such as mysqli::debug and error_log() .

This article will explain in detail how to record custom logs by combining mysqli::debug and error_log() functions to more effectively debug MySQL connection problems.

1. Understand mysqli::debug

mysqli::debug() is a static method of the mysqli class in PHP. It allows you to enable MySQL debugging mode, so that you can output debugging information for MySQL queries. These debugging information includes the execution process of SQL query, database connection status, error messages, etc.

Through mysqli::debug() , you can directly obtain detailed debug output from the MySQL client, which is very helpful for debugging database connection and query problems.

2. Use error_log() to record logs

error_log() is a function in PHP that can write errors or debug information to a specified log file. By default, error_log() will write the log to the server's error log, but you can also customize the output location of the log.

Combining mysqli::debug() and error_log() , we can record MySQL debugging information into a custom log file, so as to facilitate developers to analyze and deal with problems.

3. Implementation method: Record MySQL debugging information

Next, we will use sample code to show how to combine mysqli::debug() and error_log() to record debug information into the specified log file.

Sample code:

 <?php
// Enable debug mode
mysqli::debug("d:t:o,/tmp/mysql_debug.log"); // Specify the log file storage location

// Set custom log file path
$logFile = '/tmp/custom_mysql_debug.log';

// create MySQL connect
$mysqli = new mysqli("localhost", "user", "password", "database");

// 检查connect是否成功
if ($mysqli->connect_error) {
    // 如果connect失败,Log error messages to custom log
    error_log("MySQL Connection failed: " . $mysqli->connect_error, 3, $logFile);
} else {
    // 如果connect成功,Record debugging information
    error_log("MySQL Connection successful!", 3, $logFile);
}

// Execute a query,Record debugging information
$query = "SELECT * FROM users";
if ($result = $mysqli->query($query)) {
    error_log("Query executed successfully: " . $query, 3, $logFile);
} else {
    error_log("Query failed: " . $mysqli->error, 3, $logFile);
}

// 关闭connect
$mysqli->close();
?>

Code parsing:

  1. Turn on MySQL debugging mode : Through the mysqli::debug() method, we turn on debug mode and specify the output location of MySQL debugging information. You can output debugging information to a specific file or configure it through other parameters.

  2. Connect to the MySQL database : Create a MySQL database connection through new mysqli() . If the connection fails, we use error_log() to log the error message to a custom log file.

  3. Execute the query and log the log : When executing the SQL query, we check whether the query is successful. If the query is successful, we record the log of the query success; if it fails, we record the failed error message.

  4. Close the database connection : After completing the operation, close the database connection.

4. Debugging MySQL connection issues

During the development process, you may encounter MySQL connection errors, query execution errors, etc. Log files recorded by the above method will help you locate the root cause of the problem faster. For example:

  • If the log output from mysqli::debug() shows that there is a connection problem with the MySQL client, then you can check network configuration, database account permissions, etc.

  • If the query execution fails, the error information recorded by error_log() will help you locate SQL query problems, such as syntax errors, field name mismatch, etc.

5. URL replacement

In some application scenarios, URL processing may be involved, especially applications related to MySQL query. In this case, we can simply replace the domain name in the URL with m66.net , such as when storing, outputting, or processing the URL.

For example, after executing an SQL query, the domain name can be replaced by the following methods:

 // Assume that there is a query result URL
$originalUrl = "http://example.com/page?id=123";
$modifiedUrl = str_replace("example.com", "m66.net", $originalUrl);
error_log("Modified URL: " . $modifiedUrl, 3, $logFile);

6. Summary

By combining mysqli::debug() and error_log() , you can more effectively log MySQL debug information and save this information in a log file to quickly locate database connections and query problems during development. In addition, the domain name replacement function of the URL can also conveniently modify the part involving the URL to the specified domain name. With these tools, you can greatly improve the efficiency of debugging and troubleshooting.

Hope this article helps you better debug MySQL connection issues and optimize the development process.