Current Location: Home> Latest Articles> Integrate mysqli::debug information into custom debug panel

Integrate mysqli::debug information into custom debug panel

M66 2025-06-01

In PHP, it is very important to debug SQL statements and connect problems when using mysqli for database operations. The mysqli class provides a debug method that can help us output detailed debugging information, but by default its output is relatively scattered and difficult to integrate and view. This article will introduce how to capture and integrate the information output from mysqli::debug into a custom debug panel to improve debugging efficiency.

1. Understand how mysqli::debug works

mysqli::debug is a static method provided by the mysqli extension, which can write debug information to the specified file or output target. Its typical usage is:

 mysqli::debug('d:t:O,/path/to/debug.log');

Where the parameter is a string that specifies the category and output path of the debug information. For example:

  • d:t:O represents the debug level and output target (for example, output directly to the screen)

  • /path/to/debug.log means writing debug information to the specified file

By default, mysqli::debug will output a lot of underlying debugging information, which is easy to locate connection or query problems.

2. Capture mysqli debug information to variables

mysqli::debug outputs information directly by default, and does not support direct return of strings. To integrate this information into a custom debug panel, it must be "captured" for subsequent processing and display.

The method is to capture the output with the help of PHP's output buffering mechanisms ob_start() and ob_get_clean() :

 // Turn on output buffering
ob_start();

// start up mysqli Debug output
mysqli::debug('d:t:O');

// Get the buffer content
$debugOutput = ob_get_clean();

In this way, $debugOutput contains debug information, which we can save, format or pass to the debug panel.

3. Combining mysqli connection and debugging information

The following is a demonstration code that integrates the mysqli connection process and debugging information:

 <?php
// start upOutput缓冲捕获 mysqli::debug Output
ob_start();
mysqli::debug('d:t:O');  // 设定Debug output到当前缓冲区
$debugInfo = ob_get_clean();

// Connect to the database
$mysqli = new mysqli('localhost', 'user', 'password', 'database');

if ($mysqli->connect_error) {
    echo "Connection failed: " . $mysqli->connect_error;
} else {
    echo "Connection successfully!";
}

// Pass debug information to a custom debug panel(Simple print here)
echo "<pre style='background:#f0f0f0;padding:10px;border:1px solid #ccc;'>";
echo "MySQLi Debug information:\n";
echo htmlspecialchars($debugInfo);
echo "</pre>";

4. Integrate debug information into custom panel

If you have a web-based debugging panel, you can dynamically inject the captured debugging information into the panel in this way:

 <?php
// Assumptions $debugPanel is a debug panel object orHTMLcontainer
function addDebugInfoToPanel($info) {
    // You can format or add styles according to your needs
    $formatted = "<div class='debug-info' style='font-family: monospace; background:#222; color:#0f0; padding:10px; margin:10px 0;'>"
                 . nl2br(htmlspecialchars($info)) . "</div>";
    echo $formatted;
}

// 捕获并Output调试information
ob_start();
mysqli::debug('d:t:O');
$debugOutput = ob_get_clean();

addDebugInfoToPanel($debugOutput);

In this way, the debugging information will be displayed in your debugging panel in a stylised and readable form.

5. Further expansion: debug information file management

If you want to write debug information to a file for analysis, you can specify the parameters of mysqli::debug :

 mysqli::debug('d:t:/var/log/mysqli_debug.log');

Combined with the PHP script cycle, the log file content is read regularly and pushed to the debug panel for convenient history tracking.

6. Things to note

  • The debugging information may contain sensitive data and is not recommended to enable it in a production environment.

  • The output buffer capture method is suitable for rapid development and debugging. It is recommended to use a special log management system for complex projects.

  • Remember to adjust file permissions according to the actual environment to ensure that the log files are writable.

Summarize

By capturing the debug output of mysqli::debug using PHP's output buffering function, we can flexibly integrate these underlying debug information into a custom debug panel. This greatly improves the efficiency of problem investigation and positioning, and makes the debugging process of database operations more intuitive and efficient.