Page 1 of 1

Add All Together - PHP & MySQL

Posted: Wed Mar 16, 2011 1:53 pm
by micky007
Hi Guys,

I need some advice please. In my database table called 'affleads' i have a row called Payout. Now below is the following MySQL code i am using on my PHP script:

Code: Select all

$query = "SELECT Payout FROM affleads WHERE AffID ='$affid'";
Now lets say there are 6 records matching the SQL query as the following:

[text]2.50
2.34
1.85
2.74
1.00
2.07[/text]

What PHP code would i use in order to get the values of Payout and then Add them all together and save the value to a variable?

Any help would be great and thank you in advance.

Thanks!

Re: Add All Together - PHP & MySQL

Posted: Wed Mar 16, 2011 1:58 pm
by Jonah Bron
Loop through the query results as usual, but add up the total.

Code: Select all

$total = 0;
while ($row = mysql_fetch_object($result)) {
    $total += $row->Payout;
}

Re: Add All Together - PHP & MySQL

Posted: Wed Mar 16, 2011 2:02 pm
by AbraCadaver
Or just do it in the query:

Code: Select all

$query = "SELECT SUM(Payout) as Payout FROM affleads WHERE AffID ='$affid'";

Re: Add All Together - PHP & MySQL

Posted: Wed Mar 16, 2011 2:32 pm
by micky007
Hi,

Thank you for your reply and help, although I've just been thinking and have another problem. Below is what table looks like:

Image

What I'd like to do is do it so my PHP script will output the total number of records for each Program Name and output the Total Payout. So for example, going off the records above the output would be the following:
Nuffield Health - 3 - £7.50
I was thinking of using the DISTINCT method in SQL but then relised it wouldnt work to how i want it. Any help please guys would be great.

Thank you very much guys!