What is the alternative method to summing values from a database query in PHP?

When summing values from a database query in PHP, an alternative method is to use the `SUM()` function directly in the SQL query itself. This way, the database server performs the sum operation and returns the result, reducing the amount of data transferred between the database and the PHP script.

<?php
// Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare SQL query with SUM() function
$sql = "SELECT SUM(column_name) AS total_sum FROM table_name";

// Execute query
$stmt = $pdo->query($sql);

// Fetch the result
$result = $stmt->fetch(PDO::FETCH_ASSOC);

// Output the sum
echo "Total sum: " . $result['total_sum'];
?>