php - 如何将 mysql 数字数据与另一个 mysql 数字数据相加

标签 php mysql

<?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/

相关文章:

php - "prototype"的项目 "Array"不存在

php - Oauth2有没有Php客户端?

PHP:从扩展类的方法访问父类的静态变量

php - 将标签添加到数据库值

mysql - 如何知道 float 中小数点右边的位数?

MySQL 将 secure-file-priv 设置为多个目录

c# - 搜索时 SQL 查询出错

php - Where 子句 php 不起作用

PHP MYSQL 关联数组和表

php - 检测模型的变化; php yii 框架