info
This article covers the mysqli_info() function in PHP, which returns a formatted string containing status information about the most recently executed query.
Introduction to the mysqli_info() function
The mysqli_info() function is a built-in PHP function that returns a formatted string describing the status of the most recently executed query. For example, an INSERT statement typically returns a string like Records: 1 Duplicates: 0 Warnings: 0. If you need the exact count of affected rows, use mysqli_affected_rows() instead.
How to use the mysqli_info() function
To use mysqli_info(), call it on a valid MySQLi connection after executing a query. Here is an example with basic error handling:
How to use the mysqli_info() function?
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$result = mysqli_query($mysqli, "INSERT INTO table_name (column1, column2, column3) VALUES ('value1', 'value2', 'value3')");
if ($result) {
$info = mysqli_info($mysqli);
echo "Query information: " . $info;
} else {
echo "Query failed: " . mysqli_error($mysqli);
}
mysqli_close($mysqli);
?>In this example, we establish a connection to a MySQL database and check for connection errors. We then execute an INSERT query and verify its success before calling mysqli_info(). The function returns a formatted status string, which we output using echo. Finally, we close the connection.
Conclusion
The mysqli_info() function provides a quick way to retrieve formatted status details about the most recently executed query. Use it alongside other MySQLi functions to monitor query execution and handle database operations effectively.
Practice
Which of the following statements about PHP are correct according to the information provided on the webpage?