PHP - MYSQL : sum() operation
Last Updated :
01 Oct, 2021
Problem Statement:
In this article, we are going to perform sum() aggregate operation on our database using PHP with xampp server.
So we are considering the food_order database and perform database sum() operation.
Requirements:
xampp
Introduction:
PHP stands for hypertext preprocessor which is a server-side scripting language connect with databases.
it is connected with MySQL to perform manipulations in a database.
MySQL is a database query language for managing databases.
sum() function is an aggregation operation used to add the particular column based on the given condition.
Syntax:
SELECT SUM(column1),column2,...column1 from
table_name;
It can be also used with GROUP BY & HAVING clause.
Example query:
Consider the table:
Find the total cost of food items
SELECT SUM(cost) from food;
Output:
Total cost: 2935
Find the total weight of food items
SELECT SUM(weight) from food;
Total weight: 382
Find the total cost of food items with respect to items
SELECT food_item, SUM(cost) FROM food GROUP BY food_item;
Total cakes = 1365
Total chocos = 200
Total chocoss = 705
Total fry = 345
Total milk = 320
Approach
- Create a database
- Create a table
- PHP code to perform sum() operation
Steps

- Create a database named geek and create a table named food
Table structure:
Refer this for how to insert records in xampp
https://www.geeksforgeeks.org/performing-database-operations-in-xampp/
Finally, the food table contains

- PHP code to perform sum function
form.php
After writing code type "localhost/form.php" to view the output in the browser.
PHP
<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//geek is the database name
$dbname = "geek";
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "sum() aggregate Demo "; echo"</h1>";
echo "<h2>";echo " Find the total cost of food items ";echo "</h2>";
//sql query
$sql = "SELECT SUM(cost) from food";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " Total cost: ". $row['SUM(cost)'];
echo "<br>";
}
echo "<h2>";echo " Find the total weight of food items ";echo "</h2>";
//sql query
$sql = "SELECT SUM(weight) from food";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " Total weight: ". $row['SUM(weight)'];
echo "<br>";
}
//close the connection
$conn->close();
?>
</body>
</html>
Output:
Example: sum() with group by clause
form1.php
PHP
<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//geek is the database name
$dbname = "geek";
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "sum() aggregate with group by Demo "; echo"</h1>";
echo "<h2>";echo "
";echo "</h2>";
//sql query
$sql = "SELECT food_item, SUM(cost) FROM food GROUP BY food_item";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo "Total ". $row['food_item']. " = ". $row['SUM(cost)'];
echo "<br />";
}
//close the connection
$conn->close();
?>
</body>
</html>
Output:
Similar Reads
PHP | DsDeque sum() Function
The Ds\Deque::sum() function is an inbuilt function in PHP which is used to return the sum of all elements present in the Deque. Syntax: public Ds\Deque::sum( void ) : number Parameters: This function does not accept any parameter. Return Value: This function returns the sum of all Deque elements. B
2 min read
PHP array_sum() Function
The array_sum() function in PHP is used to calculate the sum of values in an array. It works with both indexed and associative arrays and can handle arrays containing both integers and floats. If the array is empty, it returns 0.number array_sum ( $array )Syntaxnumber array_sum(array $array)Paramete
2 min read
PHP DsMap sum() Function
The Ds\Map::sum() function of PHP is used to get the sum of all of the values present in the Map instance. Syntax: number public Ds\Map::sum ( void ) Parameters: This function does not accepts any parameter. Return value: This function returns the sum of all of the values present in the Map instance
1 min read
PHP DsSet sum() Function
The Ds\Set::sum() function of Ds\Set class in PHP is an inbuilt function which is used to find the sum of all of the elements present in a Set. Syntax: number public Ds\Set::sum ( void ) Parameter: This function does not accepts any parameter. Return Value: This function returns the sum of the value
1 min read
PHP | DsVector sum() Function
The Ds\Vector::sum() function is an inbuilt function in PHP which returns the sum of all the elements of the vector. Syntax: number public Ds\Vector::sum( void ) Parameters: This function does not accept any parameter. Return Value: This function returns the sum of all elements in the vector. The su
2 min read
PHP - MySQL min(),max() aggregate operations
In this article, we are going to find minimum and maximum details from the table column using PHP from MySQL Xampp server. We are taking an example from employee database to find the minimum and maximum salary of the employee. Requirements -Xampp server Introduction : PHP - It stands for Hyper Text
4 min read
MySQL | Operator precedence
Operator precedence specifies the order in which operators are evaluated when two or more operators with different precedence are adjacent in an expression. For example, 1+2/3 gives the different result as compared to (1+2)/3. Just like all other programming languages C, C++, Java etc. MySQL also ha
3 min read
PHP Operators
In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH
8 min read
PHP | DsSequence sum() Function
The Ds\Sequence::sum() function is an inbuilt function in PHP which is used to return the sum of all values of the sequence. Syntax: number abstract public Ds\Sequence::sum( void ) Parameters: This function does not accept any parameters. Return value: This function returns the sum of all sequence e
1 min read
PHP 7 | Spaceship Operator
This article will make you aware of a very useful operator i.e the spaceship operator PHP 7. The spaceship operator or combined comparison operator is denoted by "". This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands. This ope
2 min read