How can count queries in PHP be optimized for better performance?

Count queries in PHP can be optimized for better performance by using the SQL COUNT() function directly in the query instead of fetching all rows and counting them in PHP. This reduces the amount of data transferred between the database and PHP, resulting in faster query execution. Additionally, using proper indexing on the columns being counted can further improve performance.

// Example of optimizing a count query in PHP
$query = "SELECT COUNT(*) FROM table_name WHERE condition = 'value'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
$count = $row[0];
echo "Count: " . $count;