What are some alternative methods to retrieve information about affected rows and query duration in PHP?

When executing SQL queries in PHP, it is important to retrieve information about the affected rows and query duration for debugging and monitoring purposes. One way to achieve this is by using the mysqli_affected_rows() function to get the number of affected rows and measuring the time taken for the query to execute using PHP's microtime() function.

// Execute SQL query
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

// Get number of affected rows
$affectedRows = mysqli_affected_rows($connection);
echo "Affected rows: " . $affectedRows . "<br>";

// Measure query duration
$start = microtime(true);
while($row = mysqli_fetch_assoc($result)) {
    // Process each row
}
$duration = microtime(true) - $start;
echo "Query duration: " . $duration . " seconds";