What is the potential issue with summing values from a MySQL database with only 2 decimal places in PHP?

When summing values from a MySQL database with only 2 decimal places in PHP, there is a risk of losing precision due to rounding errors. To solve this issue, it is recommended to perform the sum operation on the database side using MySQL's built-in functions like SUM(). This way, the sum will be calculated with the necessary precision and accuracy.

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

// Query to sum values with precision
$query = "SELECT SUM(column_name) AS total_sum FROM table_name";

$result = mysqli_query($connection, $query);

if($result){
    $row = mysqli_fetch_assoc($result);
    $total_sum = $row['total_sum'];
    
    // Use the total_sum value with precision
    echo "Total Sum: " . number_format($total_sum, 2);
} else {
    echo "Error: " . mysqli_error($connection);
}

// Close database connection
mysqli_close($connection);