How to Sum Column in MySQL using PHP/MySQL PHP Script


How to Sum Column in MySQL using PHP/MySQL PHP Script

In this tutorial, I’m going to show you how to sum MySQL column using PHP/MySQLi. I’ve also included in this tutorial the use of GROUP BY in mysqli query and the 2 mysqli methods which I have included in the comments. This tutorial does not include a good design but will give you idea on the topic.

 

Creating our Database

First, we’re going to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “sum”.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

 

  1. CREATE TABLE `product` (
  2. `productid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `product_name` VARCHAR(30) NOT NULL,
  4. PRIMARY KEY(`productid`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  1. CREATE TABLE `sales` (
  2. `salesid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `productid` INT(11) NOT NULL,
  4. `sales_qty` DOUBLE NOT NULL,
  5. PRIMARY KEY(`salesid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

 

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *