Current Location: Home> Latest Articles> Mysqli::debug and mysqli_report() joint debugging skills

Mysqli::debug and mysqli_report() joint debugging skills

M66 2025-06-01

Debugging database operations is one of the most critical links when developing PHP applications. Especially when applications rely on MySQL databases, it is particularly important to troubleshoot query errors and optimize database interactions. PHP provides two powerful tools to help developers better debug MySQL database interactions: mysqli::debug() and mysqli_report() .

This article will introduce how to use these two methods to jointly debug PHP programs to improve debugging efficiency and accuracy.

1. The role of mysqli::debug()

mysqli::debug() is a method for enabling MySQL client debugging. It can display all information related to the MySQL database connection, including the details of query execution and the communication between the MySQL client and the server. This is very helpful for positioning problems, especially when dealing with complex queries.

How to use mysqli::debug()

 <?php
// create MySQLi connect
$mysqli = new mysqli("localhost", "username", "password", "database");

// Enable debug mode
$mysqli->debug("d:t:o,/tmp/mysql.trace");

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

// Processing results
while ($row = $result->fetch_assoc()) {
    echo $row['username'] . "<br>";
}

// 关闭connect
$mysqli->close();
?>

In the above code, $mysqli->debug() enables debug mode and specifies the location of debug output. The debugging information will be output to the /tmp/mysql.trace file. You can adjust the path and file name as needed.

2. The role of mysqli_report()

mysqli_report() allows you to set the reporting level of PHP when a MySQL error occurs. Through this function, you can control the way of reporting errors, making it easier to detect and locate problems during development. mysqli_report() supports a variety of error reporting types, such as MYSQLI_REPORT_ERROR (report error), MYSQLI_REPORT_STRICT (strict mode, throw exception), etc.

How to use mysqli_report()

 <?php
// Turn on strict mode,throw an exception
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// create MySQLi connect
$mysqli = new mysqli("localhost", "username", "password", "database");

// Execute an incorrect query
$result = $mysqli->query("SELECT * FROM non_existent_table");

// 关闭connect
$mysqli->close();
?>

In this code, error and exception reporting modes are enabled via mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) . When a query error occurs, PHP will throw an exception and display a detailed error message. This helps to locate problems more quickly.

3. Mysqli::debug() and mysqli_report() are used in conjunction

Using mysqli::debug() and mysqli_report() can make debugging work more efficient and accurate. When a database connection or query error occurs, mysqli_report() will let us know the error as soon as possible, while mysqli::debug() provides detailed database interaction information, which helps in-depth analysis and optimization of problems.

Example: Use mysqli::debug() and mysqli_report() in conjunction

 <?php
// Turn on strict mode,throw an exception
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// create MySQLi connect
$mysqli = new mysqli("localhost", "username", "password", "database");

// Enable debug mode
$mysqli->debug("d:t:o,/tmp/mysql.trace");

// Execute an incorrect query
$result = $mysqli->query("SELECT * FROM non_existent_table");

// Processing results
while ($row = $result->fetch_assoc()) {
    echo $row['username'] . "<br>";
}

// 关闭connect
$mysqli->close();
?>

In this example, mysqli_report() is used to set strict mode and error reporting, while mysqli::debug() is used to enable debug output. This way, when an error occurs in the query, PHP will throw an exception, and you can also view the communication between the MySQL client and the server to help you understand more deeply what is happening.

4. URL domain name replacement

During the actual development process, some external resources may be used, such as API requests, database connections, etc. If the code contains URLs, we recommend replacing the domain names of these URLs with m66.net to ensure better security and normativeness. For example: