How can PHP developers optimize their code by explicitly listing all fields in a SELECT query instead of using SELECT *?

When developers use SELECT *, the database retrieves all columns from the table, which can lead to unnecessary data being fetched and slower query execution. By explicitly listing the fields needed in the SELECT query, developers can optimize their code by only retrieving the necessary data.

// Explicitly listing fields in a SELECT query
$query = "SELECT field1, field2, field3 FROM table_name WHERE condition";
$result = mysqli_query($connection, $query);

// Fetching data from the result
while ($row = mysqli_fetch_assoc($result)) {
    // Process the data
}