How can the use of COUNT() and SELECT * impact the performance of SQL queries in PHP?

Using COUNT() and SELECT * in SQL queries can impact performance because COUNT() requires the database to scan all rows to count them, which can be resource-intensive. SELECT * also retrieves all columns for each row, which can be unnecessary if only specific columns are needed. To improve performance, it's better to specify the columns needed in the SELECT statement and use COUNT(*) instead of COUNT() if row count is needed.

// Example of optimizing SQL query performance by specifying columns and using COUNT(*)
$query = "SELECT id, name, email FROM users";
$result = mysqli_query($connection, $query);

if ($result) {
    $row_count = mysqli_num_rows($result);
    echo "Total rows: " . $row_count;
    
    while ($row = mysqli_fetch_assoc($result)) {
        // Process each row data
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}