0

here is my code-

$sql="SELECT * FROM payment WHERE customerid='$id'";
if ($result=mysqli_query($con,$sql))
  {
  while ($row=mysqli_fetch_assoc($result);
    {
    echo $row['fraction_payment'];
    }    
}

What im trying to do: constantly keep adding $row['fraction_payment'] with the previous value.

if database value of

fraction_payment 1 = 100 
fraction_payment 2 = 200 
fraction_payment 3 = 300

expected result will be:

$row['fraction_payment'][1] = 100
$row['fraction_payment'][2] = 300
$row['fraction_payment'][3] = 600

How should i proceed to do that in PHP?

Zils
  • 403
  • 2
  • 4
  • 19

2 Answers2

1

This should do the trick, pretty self explanatory:

$total = 0;
$sql="SELECT * FROM payment WHERE customerid='$id'";
if ($result=mysqli_query($con,$sql))
{
  while ($row=mysqli_fetch_assoc($result);
  {
    $total += $row['fraction_payment'];
    echo $total;
  }    
}
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • I think he was wanting to re-assign that back into row over the previous values (judging by his example of expected result) ? – IncredibleHat Nov 08 '17 at 15:05
1
$sql="SELECT * FROM payment WHERE customerid='$id'";
if ($result = mysqli_query($con,$sql))
{
  $sum = 0;
  $result = [];
  while ($row = mysqli_fetch_assoc($result);
  {
    $sum += $row['fraction_payment'];
    $result[] = $sum;
  }
  var_dump($result);
}

And don't forget about SQL injections: How can I prevent SQL injection in PHP?

r.1
  • 101
  • 2