mysqli::debug() is a relatively useless but very powerful debugging tool in PHP. It allows developers to enable the debugging function of the underlying MySQLi extension, thereby analyzing details in connections, query execution, etc. After PHP 8.1, the parameter support of mysqli::debug() is more flexible, especially the trace options , which provides developers with stronger customization capabilities.
This article will introduce how to use and advanced configuration of mysqli::debug() trace options, and combine some examples to help you better understand its purpose.
mysqli::debug() is a method used to enable debug output, it is actually an object-oriented version of the mysqli_debug() function. It accepts a string parameter that specifies debugging options.
mysqli::debug(string $options): bool
Among them, $options is the focus we want to focus on - a set of comma-separated debug settings, such as:
mysqli::debug("d:t:o,/tmp/client.trace")
In debug strings, several common options include:
dEnable debugging
tEnable trace (trace)
o,<file> Write debug output to the specified file
i,<file> Read configuration from the specified file
F record function call
A records all calls (more verbose)
n Display network communication content
You can combine these options to enable detailed monitoring of MySQLi execution.
Suppose you want to log debug information to the /tmp directory of the server:
<?php
mysqli::debug("d:t:o,/tmp/mysqli.trace.log");
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$result = $mysqli->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
print_r($row);
}
$mysqli->close();
?>
This configuration will:
Enable debugging ( d )
Enable tracking function ( t )
Save the output to /tmp/mysqli.trace.log
mysqli::debug("d:t:F:o,/tmp/trace_func.log");
When F parameter is enabled, the log contains a call stack for each MySQLi function, which is especially useful for analyzing performance bottlenecks.
You can set debug parameters in advance by reading the configuration file:
// config.txt content:d:t:o,/tmp/from_config.log
mysqli::debug("i,/var/www/html/config.txt");
This avoids hard coding, increases flexibility, and is suitable for large projects or debug deployment environments.
If you suspect that there is something wrong with the communication between the client and the server, you can enable n :
mysqli::debug("d:t:n:o,/tmp/net_debug.log");
Please note: The logs generated by this option may contain sensitive data and should be strictly protected.
After the debug log is generated, you can view it through the following command:
tail -f /tmp/mysqli.trace.log
When using in production environments, it is recommended to replace the log file path with a dedicated log directory and set appropriate permissions.
Combined with front-end URLs, you can even associate logs with trace ID, such as:
$traceId = uniqid("trace_", true);
mysqli::debug("d:t:o,/tmp/$traceId.log");
header("X-Debug-Trace: https://m66.net/debug/$traceId.log");
In this way, you can get tracking links in the browser developer tools and quickly locate problems.
Mysqli::debug() 's trace options provide developers with powerful debugging methods. By configuring the trace string reasonably, you can achieve:
Function call analysis
Network debugging
Centralized log management
Combined with the scalability of configuration files
During the development and testing phase, making good use of these trace options will not only save a lot of troubleshooting time, but also help you understand the interaction mechanism between PHP and MySQL more deeply.