What are the common pitfalls when performing calculations in PHP using data retrieved from MySQL?

One common pitfall when performing calculations in PHP using data retrieved from MySQL is not properly handling data types. Ensure that the data retrieved from MySQL is of the correct type (e.g., integer, float) before performing calculations to avoid unexpected results. Additionally, be mindful of potential null values that could affect the outcome of calculations.

// Example code snippet to handle data types when performing calculations with MySQL data
$result = $mysqli->query("SELECT * FROM table");
$row = $result->fetch_assoc();

// Ensure data types are correct before calculations
$number1 = (int)$row['number1'];
$number2 = (float)$row['number2'];

// Perform calculations
$total = $number1 + $number2;

echo $total;