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];
精彩评论