How can a loop in PHP be used to increment a counter variable for each row fetched from a MySQL query result?

To increment a counter variable for each row fetched from a MySQL query result in PHP, you can use a loop to iterate over the result set and increment the counter variable within the loop. This allows you to keep track of the number of rows fetched from the query result.

// Assuming $conn is the MySQL database connection and $query is the SQL query
$result = mysqli_query($conn, $query);

// Initialize counter variable
$counter = 0;

// Loop through the query result and increment counter for each row
while ($row = mysqli_fetch_assoc($result)) {
    // Process each row as needed
    $counter++;
}

echo "Total rows fetched: " . $counter;