What are some common methods for adding up data records from a MySQL table in PHP?

When working with MySQL tables in PHP, you may need to add up data records from a specific column. One common method to achieve this is by using a SQL query with the SUM() function to calculate the total sum of the desired column. You can then fetch the result using PHP and use it in your application as needed.

// Connect to MySQL database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// Query to sum up data records from a specific column
$query = "SELECT SUM(column_name) AS total_sum FROM table_name";
$result = mysqli_query($connection, $query);

// Fetch the result
$row = mysqli_fetch_assoc($result);
$totalSum = $row['total_sum'];

// Output the total sum
echo "Total Sum: " . $totalSum;

// Close the connection
mysqli_close($connection);