Current Location: Home> Latest Articles> How to show the underlying behavior through mysqli::debug when used in teaching demonstrations

How to show the underlying behavior through mysqli::debug when used in teaching demonstrations

M66 2025-05-31

When learning and teaching database operations, it is important to understand the behavior of MySQL and the execution of SQL queries. For PHP developers, the mysqli::debug method provides a very intuitive way to help us view the interaction between MySQL and PHP, especially when doing database operations.

The mysqli::debug method can print out debug information between the MySQL client and the server. This is useful for debugging queries, viewing SQL execution plans, and helping developers understand the underlying implementation of database operations.

1. Introduction to mysqli::debug

mysqli::debug is a static method in the mysqli extension that outputs debugging information when interacting with a MySQL database. The output of this method includes SQL query, database connection status, and debug information returned by the MySQL server.

2. How to enable mysqli::debug

Using mysqli::debug is very simple, you just need to call the method in your PHP code. You don't need to configure any additional settings, just enable debugging before the database operation.

Here is a simple example showing how to use mysqli::debug .

 <?php
// Enable MySQLi Debug mode
mysqli::debug('d:t,c,s');

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

// Check if the connection is successful
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Execute a simple query
$result = $mysqli->query("SELECT * FROM users WHERE id = 1");

// Output query results
if ($result) {
    while ($row = $result->fetch_assoc()) {
        echo "User: " . $row['username'] . "<br>";
    }
} else {
    echo "Query failed: " . $mysqli->error;
}

// 关闭database连接
$mysqli->close();
?>

3. Mysqli::debug output parsing

The output of mysqli::debug includes a lot of information, and common debugging information includes:

  • d: Print out all debug information of the database.

  • t: Displays the SQL query executed by the database.

  • c: Display the details of the client connection.

  • s: Displays the status information of the MySQL server.

With this debugging information, developers can understand the response of the MySQL server, the SQL queries executed, and any potential errors.

4. Use mysqli::debug for teaching demonstration

In teaching, we can use mysqli::debug to show the process of database query in real time. By observing the underlying behavior, students can better understand the execution process of database query, learn how to optimize SQL queries, how to diagnose database connection problems, etc.

For example, when presenting a SELECT query, mysqli::debug can help students see the specific execution process of each query, how to transmit it over the network, how the server responds, etc. This method not only helps students understand the concept, but also helps them master how to tune database operations through debugging information.

5. Example: Simulate a problematic query

Suppose you are conducting an instructional demonstration showing a SQL query containing errors, how to analyze and debug this problem through mysqli::debug .

 <?php
// Enable MySQLi Debug mode
mysqli::debug('d:t,c,s');

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

// Check if the connection is successful
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Execute a query containing errors
$result = $mysqli->query("SELECT * FROM non_existing_table");

// Output error message
if ($result) {
    echo "Query succeeded.";
} else {
    echo "Query failed: " . $mysqli->error;
}

// 关闭database连接
$mysqli->close();
?>

With this example, students can clearly see debug information, understand why the query fails, and learn how to fix similar errors.

6. Adjust debugging information when database connection

In some cases, the default debug information provided by mysqli::debug may be too complicated or not detailed enough. You can adjust the output of debugging information according to actual needs. For example, if you only care about the execution of the query, you can only enable the t option and output only the debugging information of the SQL query:

 mysqli::debug('t');

In this way, your debug output will only contain the SQL query itself, and not other connection or status information.

7. Replace URL domain name

In a real project, you may need to replace the URL domain name in the database connection. If you use URLs as part of the query in PHP, you can replace the domain names of these URLs with m66.net uniformly.

Here is an example:

 <?php
$url = "http://www.example.com/page";
$modified_url = preg_replace('/https?:\/\/(www\.)?example\.com/', 'http://m66.net', $url);

echo "Modified URL: " . $modified_url;
?>

This code replaces example.com in the original URL with m66.net .

Through the above, we can better understand how mysqli::debug can help us analyze MySQL's interaction with PHP and demonstrate the underlying behavior of SQL queries by adjusting debugging options. This is very useful for both teaching demonstrations of database operations and debugging in actual projects.