-1

I need to know how I can get the sum of a column in a MySQL database using PHP

This is a list of products in a DB and each one has a different price, so I want to calculate the sum and show them somewhere on my panel page. How can I do this and store the result in a variable?

MyPHPAdmin image showing the price column

theob
  • 199
  • 1
  • 10
  • Possible duplicate of [MySql sum elements of a column](https://stackoverflow.com/questions/4586040/mysql-sum-elements-of-a-column) – mooga Jun 16 '18 at 15:38

1 Answers1

0

You can completely handle it in the MySQL query:

SELECT SUM(column_name) FROM table_name;

Using PDO

$stmt = $handler->prepare("SELECT SUM(value) AS value_sum FROM codes");
$stmt->execute();

$row = $handler->fetchAll(PDO::FETCH_OBJ);
$sum = $row->value_sum;
Sahipsiz
  • 105
  • 1
  • 11