In what situations would it be more efficient to use MySQL for calculations instead of PHP?

When dealing with large datasets or complex calculations, it may be more efficient to use MySQL for calculations instead of PHP. This is because MySQL is optimized for handling large amounts of data and performing calculations efficiently. By moving the calculations to the database level, you can reduce the amount of data transferred between the database and the PHP application, leading to better performance.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Perform calculation in MySQL
$query = "SELECT SUM(column_name) AS total FROM table_name";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
$total = $row['total'];

// Output the result
echo "Total: " . $total;