user2924416
user2924416

Reputation: 5

SUM of a column doesnt work it just print 0

<?php
  $con=mysqli_connect("localhost","root","root","test");
  // Check connection
  if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $results = mysqli_query($con,"SELECT SUM(jumlah) AS jumlah_sum FROM finance_income"); 
  echo '<center>';

 /*while($rows = mysqli_fetch_array($results)); 
 { *?
     echo "<p>" . "Rp " . number_format($rows['jumlah_sum']) . "</p>";
 /*}*/
 echo "</center>";

 mysqli_close($con);

?>

That was my code, i think it's working but when i open it in my mamp it just printed 0 not 2,800,000, 2,800,000 is the sum of colum "jumlah"

id timestamp________ nama cat_id jumlah

1 2013-10-26 14:08:18 test Iuran 400000

2 2013-10-26 22:06:35 test Donasi 2000000

3 2013-10-27 10:51:32 test Iuran 100000

4 2013-10-27 10:55:01 test Iuran 100000

5 2013-10-27 11:01:38 test Iuran 100000

6 2013-10-27 11:02:07 test Iuran 100000

EDIT: I tried removing the while and it works thanks

Upvotes: 0

Views: 83

Answers (1)

M I
M I

Reputation: 3682

This is working code.

<?php
  $con=mysqli_connect("localhost","root","root","test");
  // Check connection
  if (mysqli_connect_errno())
  {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $results = mysqli_query($con,"SELECT SUM(jumlah) AS jumlah_sum FROM finance_income"); 
  echo '<center>';
  $rows = mysqli_fetch_array($results);
  echo "<p>" . "Rp " . number_format($rows['jumlah_sum']) . "</p>";
  echo "</center>";
     mysqli_close($con);

    ?>

Upvotes: 1

Related Questions