Skip to main content

Calculate Sum (Total) of Column in PHP


Hi guys,

The purpose of this code is to teach you on how to calculate the values of a column using the Sum function.

This is very useful if you want to create a poin of sale system in your PHP project.

Hope you will like this source code, for more question about this code email me at [email protected] thank you.

databasename: db folder sum.sql

Sample code:

  1. <?php
  2. include('header.php');
  3. ?>
  5. <body>
  6. <br>
  7. <br>
  8. <div class="container">
  9. <div class="alert alert-success">Sum the Numbers in a Column</div>
  10. <br />
  11. <table class="table table-striped table-bordered">
  12. <thead>
  13. <tr>
  14. <th>Product Name</th>
  15. <th>Quantity</th>
  16. <th>Price</th>
  17. <th>Amount</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. <?php
  22. $query=mysql_query("select * from product")or die(mysql_error());
  23. while($row=mysql_fetch_array($query)){
  24. $id=$row['product_id'];
  25. ?>
  26. <tr>
  27. <td><?php echo $row['name'] ?></td>
  28. <td><?php echo $row['qty'] ?></td>
  29. <td><?php echo $row['Price'] ?></td>
  30. <td><?php echo $row['amount'] ?></td>
  31. </tr>
  32. <?php } ?>
  33. </tbody>
  34. </table>
  35. <?php
  36. $result = mysql_query("SELECT sum(amount) FROM product") or die(mysql_error());
  37. while ($rows = mysql_fetch_array($result)) {
  38. ?>
  39. <div class="pull-right">
  40. <div class="span">
  41. <div class="alert alert-success"><i class="icon-credit-card icon-large"></i>&nbsp;Total:&nbsp;<?php echo $rows['sum(amount)']; ?></div>
  42. </div>
  43. </div>
  44. <?php }
  46. $result1 = mysql_query("SELECT sum(qty) FROM product") or die(mysql_error());
  47. while ($rows1 = mysql_fetch_array($result1)) {
  48. ?>
  49. <div class="pull-right">
  50. <div class="span">
  51. <div class="alert alert-info"><i class="icon-credit-card icon-large"></i>&nbsp;Total number of Items:&nbsp;<?php echo $rows1['sum(qty)']; ?></div>
  52. </div>
  53. </div>
  54. <?php } ?>
  55. </div>
  56. </body>
  57. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Submitted byRavi Barad (not verified)on Sun, 03/16/2014 - 21:23

Hello Sir,
in above php code, what is in header.php file....?
its a just connection file ?
or anything
can u send me code of this file?

Submitted byakash dsr (not verified)on Fri, 05/08/2015 - 10:43

program for sql sum...

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.