我需要知道如何使用PHP获取MySQL数据库中列的总和
这是数据库中的产品列表,每个产品都有不同的价格,所以我想计算总和并在面板页面的某个位置显示它们。我该怎么做并将结果存储在变量中?
您可以在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;