Current Location: Home> Latest Articles> How to close mysqli::debug debug log output

How to close mysqli::debug debug log output

M66 2025-06-01

When using PHP's mysqli extension to develop database-related applications, the mysqli::debug() method can be used to output debugging information, which is very helpful for troubleshooting problems during connection and execution. However, in a production environment, turning on the debug log will not only affect performance, but may also leak sensitive information. Therefore, it is very necessary to understand how to debug log output.

What is mysqli::debug() ?

mysqli::debug() is a method used to control the debugging function of the libmysqlclient library. After it is called, the MySQL client library outputs the specified debug information into the log file. For example:

 mysqli::debug("d:t:o,/tmp/client.trace");

This will enable the debugging function and output the debugging information to the /tmp/client.trace file.

Close the debug output of mysqli::debug()

To turn off debug output, there are two key points to note:

1. Do not call mysqli::debug()

The most direct way is not to call the mysqli::debug() method. If you use it in your test code, make sure it is commented or removed when deploying the production environment.

2. Call with an empty string

The official PHP manual states that if mysqli::debug("") is called, you can reset the debug configuration . Although this will not clear the log file contents, subsequent debug output can be stopped.

 mysqli::debug("");

?? But please note that whether this method works depends on the implementation of the underlying MySQL client library, and some versions may still write to the log file.

3. Modify php.ini configuration (applicable to libmysqlclient)

If you are using the mysqli extension based on libmysqlclient , you can also control the debugging behavior by setting the MYSQL_DEBUG environment variable:

 ; php.ini
mysqlnd.debug = ""

Or set when starting PHP:

 export MYSQL_DEBUG=""
php yourscript.php

This will allow PHP to boot without any debugging configuration, thus completely shutting down debug output.

Example: A secure database connection code (debug is not enabled)

 <?php
// Nothing is enabled mysqli Debugging configuration
mysqli::debug("");

// Create a database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check if the connection is successful
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Execute a query
$result = $mysqli->query("SELECT * FROM users");

// Processing results
while ($row = $result->fetch_assoc()) {
    echo "username:" . $row["username"] . "<br>";
}

// Close the connection
$mysqli->close();
?>

Summarize

The key to closing the mysqli::debug() output debug log is to avoid calling debug functions or clearing environment variable configurations . Debug logs are free to troubleshoot problems in development environments, but in production environments, be sure to ensure that all debug outputs are shut down for improved security and performance.