<?php
$new_result = mysqli_query($con, "SELECT input_cost FROM data_input");
while ($new = mysqli_fetch_array($new_result)) {
$sum += $new['input_cost'];
}
echo "<h2> total cost of this month is $".$sum. "</h2>";
?>
但是结果说
<br>
Notice: Undefined variable: sum in C:\xampp\htdocs\work_shop\back_end\data_input_output\result.php on line 57
<br>
这个月的总费用是 $300
这是正确的结果....
<br>
我该如何解决这个问题...??
最佳答案
您需要在循环外定义$sum
变量。试试这个-
<?php
$sum = 0; // define sum outside loop
$new_result = mysqli_query($con, "SELECT input_cost FROM data_input");
while ($new = mysqli_fetch_array($new_result)) {
$sum += $new['input_cost'];
}
echo "<h2> total cost of this month is $".$sum. "</h2>";
?>
关于php - 如何将 mysql 数字数据与另一个 mysql 数字数据相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30664186/