Current Location: Home> Latest Articles> The difference between using mysqli::debug in CLI and web environments

The difference between using mysqli::debug in CLI and web environments

M66 2025-06-01

In PHP, the mysqli extension provides a powerful tool for MySQL database operations. Mysqli::debug is one of the very useful functions that can help us debug during development. However, when you use this function in different run environments, there will be some differences. In this article, we will discuss the difference when using mysqli::debug in command line interface (CLI) and web environments, and explore how to debug based on different environments.

What is mysqli::debug ?

mysqli::debug is a static method that enables debug mode of MySQL. This method allows us to output MySQL debugging information and helps us diagnose problems during query execution.

grammar

 mysqli::debug(string $message);

Sample code

 <?php
// Enable debug mode
mysqli::debug('client_trace=1; record=1');
?>

Debug output content

When debug mode is enabled, MySQL outputs a large amount of debugging information, including query logs, error messages, and connection information. This information can help developers understand the details of query execution.

mysqli::debug in CLI environment

In the command line interface (CLI) environment, the output of mysqli::debug will be displayed directly in the command line terminal. When we run PHP scripts in a CLI environment, debug information is usually output directly to standard output.

Example

Suppose we execute a PHP script as follows:

 <?php
mysqli::debug('client_trace=1; record=1');
$conn = new mysqli('localhost', 'root', '', 'test');
$conn->query("SELECT * FROM users");
?>

When the script is executed in the CLI environment, the debugging information will be displayed on the terminal.

advantage

  • Debugging information in a CLI environment is very easy to obtain and is displayed directly in the console.

  • You can view all database operations and debugging information in real time to help developers quickly locate problems.

Things to note

  • Since CLI scripts usually do not involve logging of the web server, debug information is displayed directly on the terminal, but additional configuration is required if you want to save debug information to a file.

mysqli::debug in a web environment

In a web environment, the output of mysqli::debug is not displayed directly in the browser. Web environments usually handle requests through web servers such as Apache or Nginx, which means debugging information is logged into the web server's log file and is not displayed directly to the user.

Example

 <?php
mysqli::debug('client_trace=1; record=1');
$conn = new mysqli('localhost', 'root', '', 'test');
$conn->query("SELECT * FROM users");
?>

When the script is executed in a web environment, debug information is written to the error log of the web server and does not appear in the browser's output.

advantage

  • Debugging information in the web environment will not be directly exposed to the user, which enhances security.

  • Debugging information can be saved to log files for easier subsequent viewing and analysis.

Things to note

  • You must view the error log of the web server (such as Apache's error_log ) to see debug information.

  • If you need to output debugging information to a specific place (such as a file), it can be achieved through custom configuration.

How to debug according to different environments

1. Debug in the CLI environment

The CLI environment is one of the most commonly used debugging environments during development, and you can view all debug output directly from the terminal. If you want to enable detailed debugging in the CLI environment, you can use the following code:

 <?php
if (php_sapi_name() == 'cli') {
    mysqli::debug('client_trace=1; record=1');
    // Perform database operations
    $conn = new mysqli('localhost', 'root', '', 'test');
    $conn->query("SELECT * FROM users");
}
?>

Here, we use the php_sapi_name() function to determine whether the current environment is CLI. Only in the CLI environment can mysqli::debug be enabled to output debugging information.

2. Debug in a web environment

Debugging information in a web environment is usually recorded in a log file. If you want to view debug information in a web environment, you can redirect the debug output to a log file, or view the error log of the web server. For example:

 <?php
if (php_sapi_name() != 'cli') {
    mysqli::debug('client_trace=1; record=1');
    // Perform database operations
    $conn = new mysqli('localhost', 'root', '', 'test');
    $conn->query("SELECT * FROM users");
}
?>

Doing this ensures that debug information is output and recorded only in a web environment.

summary

To summarize, mysqli::debug is used differently in CLI and web environments. In the CLI environment, debug information is output directly to the terminal, while in the Web environment, debug information is usually recorded in the log of the Web server. Depending on different operating environments, we can use conditional statements to decide whether to enable debug information, thereby ensuring effective debugging in different environments.

During debugging, remember not to enable debug mode in production to avoid leaking sensitive information. Use mysqli::debug only in development and testing environments and ensure that log information is visible to developers only.