In PHP, what are the advantages and disadvantages of using COUNT(*) versus mysql_fetch_assoc for performance in database queries?

When it comes to performance in database queries, using COUNT(*) is generally faster than retrieving all rows with mysql_fetch_assoc. This is because COUNT(*) simply counts the number of rows that match the specified condition, while mysql_fetch_assoc retrieves all rows and stores them in memory. However, if you need to access the actual data in the rows, using mysql_fetch_assoc is necessary.

// Using COUNT(*) for performance
$query = "SELECT COUNT(*) as total_rows FROM table_name WHERE condition";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$total_rows = $row['total_rows'];