Complete newbie - How do I calculate the total from mysql?
Moderator: General Moderators
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
Complete newbie - How do I calculate the total from mysql?
Hi,
I would like to total up the value within my mysql table called dedicated and the row called value.
Can anyone help me.
thanks,
Mark
I would like to total up the value within my mysql table called dedicated and the row called value.
Can anyone help me.
thanks,
Mark
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Code: Select all
$total = 0;
while ($stuff = mysql_fetch_assoc($query))
{
$total += $stuff[value];
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
It can be done within mysql itself.
Code: Select all
$query = "SELECT SUM(value) AS tot FROM `tablename`";- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
Hi,
this is the code that I have;
It simply just prints out the SELECT SUM(value) AS tot FROM dedicated"; part
Please help
this is the code that I have;
Code: Select all
<?
$total = SELECT SUM(value) AS tot FROM dedicated";
echo $total
?>Please help
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
This is pretty basic stuff
Code: Select all
$sql = "SELECT SUM(value) AS tot FROM dedicated";
$result = mysql_query($sql) or die(mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
echo $line['tot'];- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: