How can the GROUP BY clause be effectively used in a MySQL query within PHP to group records based on a specific column?

When using the GROUP BY clause in a MySQL query within PHP, you can group records based on a specific column by including that column in the GROUP BY clause. This allows you to aggregate data and perform calculations on groups of records rather than individual rows.

// Connect to the database
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

// Query to group records based on a specific column
$query = "SELECT column_name, COUNT(*) as count FROM table_name GROUP BY column_name";

// Execute the query
$result = $mysqli->query($query);

// Fetch and display the results
while ($row = $result->fetch_assoc()) {
    echo $row['column_name'] . ': ' . $row['count'] . '<br>';
}

// Close the connection
$mysqli->close();