Current Location: Home> Latest Articles> How to Use mysqli::$info to Monitor SQL Execution Status in Real Time

How to Use mysqli::$info to Monitor SQL Execution Status in Real Time

gitbox 2025-09-30

When developing PHP applications, monitoring the execution status of SQL queries is vital for debugging and performance optimization. The mysqli extension offers multiple ways to interact with a MySQL database, and the mysqli::$info function is a powerful tool for obtaining detailed information about the last executed SQL query. This article explains how to use mysqli::$info to monitor SQL execution in real time.

1. What is mysqli::$info?

mysqli::$info is a property of the PHP mysqli class that provides detailed information about database interactions, especially after executing a query. Through this property, developers can retrieve the execution status of SQL statements, including affected rows, warning messages, and more.

This property is mainly used in the following scenarios:

  • Retrieve statistics after executing an SQL query

  • Check for warnings during SQL execution

  • Obtain the number of affected rows (e.g., after UPDATE or DELETE operations)

2. How to Use mysqli::$info

mysqli::$info is a read-only property that returns messages related to the last executed SQL statement. Its basic usage is as follows:

<?php
// Create a database connection
$mysqli = new mysqli("localhost", "username", "password", "database_name");
<p>// Check if the connection was successful<br>
if ($mysqli->connect_error) {<br>
die("Connection failed: " . $mysqli->connect_error);<br>
}</p>
<p>// Execute an SQL query<br>
$sql = "UPDATE users SET status = 'active' WHERE last_login > '2025-01-01'";<br>
if ($mysqli->query($sql) === TRUE) {<br>
// Output SQL execution status<br>
echo "SQL executed successfully: " . $mysqli->info;<br>
} else {<br>
echo "SQL execution failed: " . $mysqli->error;<br>
}</p>
<p>// Close the connection<br>
$mysqli->close();<br>
?>

In the example above, after executing an UPDATE statement, you can use $mysqli->info to get detailed information about the SQL execution. If the query is successful, it returns information about affected rows. If the query fails, it outputs an error message.

3. Common Return Values of mysqli::$info

The return values of mysqli::$info typically include:

  • Number of affected rows: For operations like INSERT, UPDATE, or DELETE, mysqli::$info returns the number of affected rows.

  • Number of warnings: If warnings occur during execution, the returned value may indicate the count of warnings.

  • Other query information: For specific queries, mysqli::$info may return additional execution details.

Example:

$sql = "UPDATE users SET status = 'active' WHERE last_login > '2025-01-01'";
$mysqli->query($sql);
echo $mysqli->info; // Returns: Records updated: 50

In this example, mysqli::$info returns a message indicating that 50 records were updated.

4. Using mysqli::$info for Real-Time Monitoring

To achieve real-time monitoring of SQL execution status, we can continuously retrieve query information using mysqli::$info and check it after each SQL operation. This approach is particularly useful during development for debugging or performance analysis.

For example, when executing multiple SQL queries in a batch, we can monitor the effect of each operation by recording the return value of mysqli::$info after each query:

<?php
$queries = [
    "INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')",
    "UPDATE users SET status = 'inactive' WHERE last_login < '2024-01-01'",
    "DELETE FROM users WHERE id = 101"
];
<p>foreach ($queries as $query) {<br>
if ($mysqli->query($query)) {<br>
echo "Query executed successfully: " . $mysqli->info . "<br>";<br>
} else {<br>
echo "Query execution failed: " . $mysqli->error . "<br>";<br>
}<br>
}<br>
?>

In this example, each time a query is executed, the current SQL execution status is immediately output using $mysqli->info. This is very helpful for batch operations or complex transactions, allowing real-time monitoring of execution results.

5. Notes

  1. Only for the last executed SQL statement: mysqli::$info only provides feedback for the last executed SQL statement. If multiple queries are executed in the same script, it will only return information for the last query.

  2. Database connection issues: If there are problems with the database connection, mysqli::$info may not return valid information. Ensure the connection is working properly when using it.

  3. Errors and warnings: mysqli::$info is mainly for a summary of execution. For detailed error information, use the mysqli::$error property.

6. Conclusion

mysqli::$info is a very practical property that helps developers monitor the execution status of SQL queries in real time. By using this property, you can obtain detailed information after SQL operations, assisting in debugging and optimizing code. Especially in complex database operations and batch queries, mysqli::$info provides key feedback to guide developers in making necessary adjustments.

If you are building a PHP application that frequently executes SQL operations, combining mysqli::$info for real-time monitoring can greatly improve your debugging and optimization efficiency.