Current Location: Home> Latest Articles> Implementation method of outputting the result of mysqli::debug to the web page front-end

Implementation method of outputting the result of mysqli::debug to the web page front-end

M66 2025-06-04

When using MySQL databases in PHP, the mysqli extension provides a very useful debugging tool - mysqli::debug . Through it, developers can obtain detailed information about database queries, which helps troubleshoot problems and optimize database operations. This article will explain how to display the debugging information of mysqli::debug to the front end of the web page.

1. What is mysqli::debug ?

mysqli::debug is a static method of the mysqli class, which can print out the debug information of the current database connection. This information includes executed SQL queries, the execution plan of the query, the status of the database connection, etc. This information is very useful for developers, especially when debugging complex SQL queries.

For example, the following code will output detailed information about the current database operation:

 mysqli::debug("d:t"); // Here "d:t" Used to enable debug output

However, by default, the information of mysqli::debug is output to the PHP error log. If you want to display these debugging information directly to the front end of the web page, the following will explain how to achieve this.

2. How to display mysqli::debug output to the front end of the web page?

To display mysqli::debug output on the front end of the web page, we need to do the following steps:

Step 1: Enable mysqli::debug

First, we need to make sure mysqli::debug is enabled and can output debug information.

 // Enable debug output
mysqli::debug("d:t");

Step 2: Get debug information and display it

The debug information output through mysqli::debug will be displayed directly in the PHP error log by default, but to display them to the front end of the web page, we need to use an output buffering method to capture and print the debug information onto the web page.

 <?php
// Start output buffering
ob_start();

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

// Enable debug output
mysqli::debug("d:t");

// You can perform database query operations
$query = "SELECT * FROM users WHERE id = 1";
$result = $mysqli->query($query);

// Get debug information
$debug_info = ob_get_contents();

// Turn off output buffering
ob_end_clean();

// Output debug information on the web page
echo '<pre>' . htmlspecialchars($debug_info) . '</pre>';
?>

The above code takes several important steps:

  1. ob_start() starts the output buffering. All output will be temporarily saved and will not be displayed to the browser immediately.

  2. After connecting to the database and executing the query, mysqli::debug("d:t") writes debug information to the output buffer.

  3. ob_get_contents() captures all output information in the buffer and saves it to the $debug_info variable.

  4. ob_end_clean() closes the output buffering to prevent the information from being output repeatedly.

  5. Finally, we format the debugging information with the <pre> tag through the echo function and output it to the web page.

3. How to customize the format of output debugging information?

To make debug information appear more clear and easy to read on web pages, you can customize the style of debug information using HTML and CSS. Here is a simple example:

 <?php
// Start output buffering
ob_start();

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

// Enable debug output
mysqli::debug("d:t");

// Perform query operations
$query = "SELECT * FROM users WHERE id = 1";
$result = $mysqli->query($query);

// Get debug information
$debug_info = ob_get_contents();

// Turn off output buffering
ob_end_clean();

// Format debug information on the web page and output it
echo '<div style="background-color: #f4f4f4; border: 1px solid #ccc; padding: 10px;">';
echo '<h3>Debugging information:</h3>';
echo '<pre style="font-size: 14px; color: #333; background-color: #f8f8f8; padding: 15px; border: 1px dashed #ddd;">' . htmlspecialchars($debug_info) . '</pre>';
echo '</div>';
?>

This approach makes debug information look neater on the web page and makes it easier to read with a custom CSS style.

4. Things to note

  • The information output by mysqli::debug may contain sensitive database information, so be sure to use it with caution in production environments. In a production environment, it is best to disable or limit debug output.

  • When debugging information is output to the front end of the web page, make sure that no sensitive data is leaked, especially parameters or database username and password in SQL queries.

  • Debugging information can be very large, so if the amount of data is too large, you can consider saving the information into a log file instead of directly outputting it to a web page.

5. Conclusion

By enabling mysqli::debug and using output buffering, we can display detailed debug information for MySQL database operations to the front end of the web page, which is very helpful for development and debugging. Remember, before it is officially launched, be sure to turn off the debugging function to prevent leakage of sensitive information.