开发者

calculate sum total of all the figures in a column

开发者 https://www.devze.com 2023-03-07 15:48 出处:网络
开发者_开发问答 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 fi

calculate sum total of all the figures in a column

开发者_开发问答

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_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.


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'];
}


$query = "SELECT sum(column) FROM tablename";
$result = mysqli_query($connection, $query);

$row = mysqli_fetch_array($result);
$total_savings = $row[0];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号