1

What I want is to add up all values in the points column These are stored as INT(4)

However its echoing Array when I try to display result

$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));

Any help would be appreciated

1
  • rarely seen a do { } while() Commented Nov 7, 2017 at 10:04

1 Answer 1

1

1.With SQL query Itself you can do like below:-

$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.Your code will work also after below change:-

$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;

Note:- 1st one is far-better than 2nd one. So use 1st one.

Sign up to request clarification or add additional context in comments.

4 Comments

You can directly sum the points in mysql query like SELECT SUM(points) as total_points FROM table WHERE result !='2'
@AiyazKhorajia i am adding that meanwhile you said. Thanks anyway.
Diamond ! Works perfectly .. will accept answer in 7 mins when system allows me to
@ChrisYates check my edited solution once. Glad to help you :):)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.