How can you optimize PHP MySQL queries to combine multiple calculations into a single query for efficiency?

To optimize PHP MySQL queries by combining multiple calculations into a single query for efficiency, you can use MySQL's built-in functions and operations to perform calculations directly in the query. This reduces the number of queries sent to the database server and minimizes the data transfer between the server and the PHP script, resulting in improved performance.

<?php
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Perform a single query to calculate multiple values
$query = "SELECT SUM(column1) AS total1, AVG(column2) AS average2, COUNT(column3) AS count3 FROM table_name";
$result = $mysqli->query($query);

// Fetch the results
$row = $result->fetch_assoc();

// Access the calculated values
$total1 = $row['total1'];
$average2 = $row['average2'];
$count3 = $row['count3'];

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