Complete newbie - How do I calculate the total from mysql?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Complete newbie - How do I calculate the total from mysql?

Post by mhouldridge »

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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$total = 0;
while ($stuff = mysql_fetch_assoc($query))
{
    $total += $stuff[value];
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It can be done within mysql itself.

Code: Select all

$query = "SELECT SUM(value) AS tot FROM `tablename`";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yup, as d11wtq said...it can be done in the query itself and it is best to do it this way
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Hi,

this is the code that I have;

Code: Select all

<?
$total = SELECT SUM(value) AS tot FROM dedicated";
echo $total
?>
It simply just prints out the SELECT SUM(value) AS tot FROM dedicated"; part

Please help
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you need to execute the query first, return the results as an associative array, and the value you want will be in "tot" field
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

urrgh!

I dont know what you mean. Please could you outline the code.

thanks
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

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'];
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Thanks for that. I get

Unknown column 'value' in 'field list'
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

value should be the name of the coulmn you are wanting to add up
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Yes, that is the name of my column.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

if you can, post your table layout and also post the code you are using now
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Ah,

Its working now..

thanks for your help.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

:roll:
Post Reply