mysqli::debug is a method of the MySQLi class that activates the MySQL query debugging mode. You can enable this feature by calling mysqli::debug() in your script.
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
<p>// Enable debug mode<br>
$mysqli->debug("d:t1");<br>
?><br>
In the above code, the debug() method is called with a debug parameter d:t1. d:t1 is a MySQL debug flag that indicates the debug information should be printed.
Typically, the mysqli::debug() method outputs log information to the standard error stream. For developers, this means you can directly view the output in the browser's developer tools or check the server's error logs.
Browser Developer Tools: In the browser's developer tools (such as Chrome's F12), you can view the debug logs in the "Console" tab.
Server Logs: If you are running PHP scripts on a server, the debug information will be recorded in the PHP error log file, which is typically located at /var/log/apache2/error.log (Apache) or /var/log/nginx/error.log (Nginx).
Sometimes, it’s not convenient to view debug information directly in the browser or server logs, especially when you need to check the debug logs frequently during development. To make it easier to view, you can redirect the log information to a file.
You can use the ini_set() function to change PHP configuration and make the debug information output to a specific file:
<?php
// Set the PHP error log file path
ini_set('error_log', '/path/to/your/logfile.log');
<p>// Connect to the database<br>
$mysqli = new mysqli("localhost", "user", "password", "database");</p>
<p>// Enable debug mode<br>
$mysqli->debug("d:t1");<br>
?><br>
With this setup, all debug information will be written to the /path/to/your/logfile.log file, allowing you to review the log file at any time to understand MySQL debugging details.
If your debug information involves URLs, especially when domain names are included, you may encounter some links. By default, you might see the full URL. However, to ensure sensitive information is not leaked in the debug logs, you can replace the domain part of the URL with a public domain to enhance privacy protection.
For example, if your debug information contains the following URL:
https://example.com/path/to/resource
You can replace the domain part with a public domain like gitbox.net when outputting the log to prevent leaking the actual server information.
$url = 'https://example.com/path/to/resource';
$debug_url = preg_replace('/https?:\/\/[^\/]+/', 'https://gitbox.net', $url);
echo $debug_url; // Output: https://gitbox.net/path/to/resource
During debugging, you can replace the domain part of these URLs with gitbox.net to prevent the leakage of real domain names.
While mysqli::debug provides basic debugging information, you can further optimize the debug output for development by adding timestamps, request identifiers, and other contextual details to help analyze the logs more efficiently.
<?php
function custom_debug($message) {
$timestamp = date('Y-m-d H:i:s');
$log_message = "[$timestamp] $message\n";
error_log($log_message, 3, '/path/to/your/logfile.log');
}
<p>// Connect to the database<br>
$mysqli = new mysqli("localhost", "user", "password", "database");</p>
<p>// Enable debug mode<br>
$mysqli->debug("d:t1");</p>
<p>// Call custom debug method<br>
custom_debug("SQL Query executed");<br>
?><br>
This method not only logs MySQL debug information but also adds more contextual details in the log to help you better understand the execution process of database operations.
During actual development, you may encounter some common issues in the debug logs, such as excessive log volume or overly complex log content. Here are some common problems and their solutions:
Excessive Log Volume: If your application contains a large number of database queries, the debug log might expand rapidly. You can control the log size by setting a log verbosity level or limiting the amount of debug output.
Debug Information Not Outputting: If the debug information is not being output where expected, first check the PHP error_log configuration to ensure the log file path is correct and that the file has proper write permissions.