SUM mysqli查询返回数组

时间:2017-11-07 10:02:10

标签: php mysqli sum

我想要的是在点列中添加所有值 它们存储为INT(4)

然而,当我尝试显示结果时,它会回显数组

$pipq = mysqli_query($con,"SELECT `points` FROM `table` WHERE `result` !='2'");
 $pips = mysqli_fetch_assoc($pipq);
 $sum = 0;
 do {
 $sum += $pips[0];
 } while($pips = mysqli_fetch_assoc($pips));

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

1.使用SQL查询本身你可以这样做: -

$pipq = mysqli_query($con,"SELECT SUM(`points`) as total_mark FROM `table` WHERE `result` !='2'");
$pips = mysqli_fetch_assoc($pipq);
echo $pips['total_mark'];

2.您的代码也会在以下更改后生效: -

$pipq = mysqli_query($con,"SELECT `points` FROM `table` WHERE `result` !='2'");
$sum = 0;
while($pips = mysqli_fetch_assoc($pips)){ // use while only
  $sum += $pips['points']; // use column-name here
}
echo $sum;

注意: - 第一个 远远好于 ,而不是第二个。所以使用第一个。