php 计算列中所有数字的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6064434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:15:43  来源:igfitidea点击:

calculate sum total of all the figures in a column

phpmysql

提问by astymore

table products

餐桌产品

I have a table that I want to calculate the sum total of all the values in column "Price" and echo the results. I have seen MySQL statement SELECT sum(column) FROM table but I am finding it difficult to use it in PHP and echo the results.

我有一个表格,我想计算“价格”列中所有值的总和并回显结果。我见过 MySQL 语句 SELECT sum(column) FROM table,但我发现在 PHP 中使用它并回显结果很困难。

回答by Lotus Notes

mysql_connect($hostname, $username, $password);
mysql_select_db($db);

$sql = "select sum(column) from table";
$q = mysql_query($sql);
$row = mysql_fetch_array($q);

echo 'Sum: ' . $row[0];

Replace variable names and table/column names as needed.

根据需要替换变量名和表/列名。

回答by Naftali aka Neal

Do your select.

做你的选择。

Get the value.

获取价值。

Echo it.

回声吧。

Thats just as much as I can help right now since i have no idea what you tried so far.

这就是我现在所能提供的帮助,因为我不知道你到目前为止尝试了什么。

For example:

例如:

$some_q = " SELECT SUM(some_col) AS `count_col` FROM some_table";

$results = mysql_query($some_q) or die(mysql_error());

while($row = mysql_fetch_array($results)){
     echo $row['count_col'];
}

回答by GolezTrol

Do you have any database connection? If not, you should read up on using MySQL in PHP.

你有任何数据库连接吗?如果没有,您应该阅读在 PHP 中使用 MySQL 的内容

If you do already know how to connect, you can still use that same example. :)

如果您已经知道如何连接,您仍然可以使用相同的示例。:)