Calculate Sum of Column in PHP and MySQL

Calculate Sum of Column in PHP and MySQL

Introduction

Document Tracking System Free Boots...
Document Tracking System Free Bootstrap Template

The tutorial on how to calculate the sum of column in PHP and MySQL is a very helpful guide for beginners who want to perform this simple task. By following the instructions and downloading the source code in this tutorial, you will be able to calculate the sum of a column in a PHP and MySQL. This can be very useful for applications that need to perform calculations on data stored in a column-based format. The tutorial is easy to follow, and it will help you get started with this essential task. So if you want to learn how to calculate the sum of a column in PHP, be sure to check out this guide!

How To Sum Values In Rows & Columns - MS Excel

How To Sum Values In Rows & Columns - MS Excel

In most programming languages, including PHP, data is typically stored in columns. This means that data is organized into rows, and each row contains a set of values. One common use for column-based data is to perform calculations on it. For example, you might want to calculate the sum of all the values in a column. Or you might want to find the value that corresponds to a specific row number.

Objectives

By the end of this tutorial, you will be able to:

  1. Create a PHP script that connects to MySQL database.
  2. Use sum MySQL statement to compute the sum of columns in the database table.
  3. To integrate and apply the source code in your projects

Relevant Source code

In this tutorial, we will learn how to create a PHP script that will compute the sum of columns in the database table. The database that we will be using is the same database used in the previous tutorial on creating a bar graph using PHP and MySQL.

The following are the requirements for this tutorial:

  • XAMPP
  • Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
Calculate Sum of Column in PHP and MySQL - connect to database
Calculate Sum of Column in PHP and MySQL – connect to database

Line 1-6 – this is the script that will connect PHP to MySQL database. MySQLi extension (the “i” stands for improved) it is used for object oriented or procedural approach.

Line 2 – the purpose of this line is to open the connection which includes the database credentials such as the server name, database username, database password and database to connect.
<?php
$con = mysqli_connect("localhost","root","","sales");
if(!$con){
die("Connection Failed:".mysqli_connect_error());
}
?>
Calculate Sum of Column in PHP and MySQL - fetch table record
Calculate Sum of Column in PHP and MySQL – fetch table record

Line 34-41 – this is the PHP script that will fetch the records from tblsales. The output would be the list of products and sales.

<?php
$result = $con->query("SELECT * FROM tblsales");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row['product'];?></td>
<td><?php echo $row['totalsales'];?></td>
</tr>
Calculate Sum of Column in PHP and MySQL - display sum of columns
Calculate Sum of Column in PHP and MySQL – display sum of columns

Line 53-56 – this is the PHP script that includes the SUM SQL statement that will sum the columns of totalsales from tblsales table.

<?php
$results = mysqli_query($con, "SELECT sum(totalsales) FROM tblsales") or die(mysqli_error());
while($rows = mysqli_fetch_array($results)){?>
Total Sales: <?php echo $rows['sum(totalsales)']; ?>

Video Demo

Calculate Sum of Colum in PHP and MySQL
Calculate Sum of Colum in PHP and MySQL

FREE DOWNLOAD SOUR CE CODE

FREE DOWNLOAD PDF TUTORIAL

Summary

In this tutorial, we have learned how to connect PHP to MySQL using the mysqli extension and as well as to how to use the sum mysql statement in order to sum the columns in the database table.

You may download the source code as well as the database used in this article. Please watch the video presentation on how to compute the sum of columns in PHP and MySQL for a detailed reference.

We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive  free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

Related Topics and Articles:

How to Generate Random Password in PHP Free Source code and Tutorial

Email Validation in PHP Free Source code and Tutorial

Law Office Management Information System in Bootstrap and PHP Script

QR Code Generator in PHP Free Source code and Tutorial

Bar Graph in PHP and MySQL Free Source code and Tutorial

 

, , , , , , , , , ,

Post navigation