PHP + MySQL计算产品价格

问题描述 投票:-1回答:1

我需要知道如何使用PHP获取MySQL数据库中列的总和

这是数据库中的产品列表,每个产品都有不同的价格,所以我想计算总和并在面板页面的某个位置显示它们。我该怎么做并将结果存储在变量中?

MyPHPAdmin image showing the price column

php mysql
1个回答
0
投票

您可以在MySQL查询中完全处理它:

SELECT SUM(column_name) FROM table_name;

使用PDO

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

$row = $handler->fetchAll(PDO::FETCH_OBJ);
$sum = $row->value_sum;
© www.soinside.com 2019 - 2024. All rights reserved.