What is the function used to sum data from a MySQL table in PHP?

To sum data from a MySQL table in PHP, you can use the SQL SUM() function along with a query to retrieve the sum of a specific column. You can execute the query using PHP's mysqli or PDO extension to connect to the MySQL database and fetch the sum result. Here's an example PHP code snippet to sum data from a MySQL table:

<?php
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to sum data from a table
$sql = "SELECT SUM(column_name) AS total_sum FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Total sum: " . $row["total_sum"];
    }
} else {
    echo "0 results";
}

$conn->close();
?>