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.
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.
SELECT SUM(column1),column2,...column1 from table_name;
It can be also used with GROUP BY & HAVING clause.
Consider the table:
Find the total cost of food items
SELECT SUM(cost) from food;
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
- Create a database
- Create a table
- PHP code to preform sum() operation
- Start xampp server
- Create a database named geek and create a table named food
Refer this for how to insert records in xampp
Finally, the food table contains
- PHP code to perform sum function
After writing code type “localhost/form.php” to view the output in the browser.
Example: sum() with group by clause