adding up all the totals in an mysql table fields

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

greasypalm
New php-forum User
New php-forum User
Posts: 6
Joined: Tue May 06, 2003 11:33 am

adding up all the totals in an mysql table fields

Postby greasypalm » Tue May 06, 2003 11:46 am

Hi all,

How do I do something like adding up all the totals in an mysql table fields (i.e. number_of_sales)

So if 'user_table_cash' had a field called 'number_of_sales' and I done a query like
$query = "SELECT * FROM user_table_cash WHERE id='davetomas'";

and there was 10 results with the numbers 2,33,44,55,22,44,66,77,33,55

how would I add them all up to display the total amount?

Any help would be tops :)

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Tue May 06, 2003 12:00 pm

Code: Select all

$total = 0;
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$total = $total + $row["field name"];
}
echo "The total is: $total ";


Regards,
Andrew

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Tue May 06, 2003 12:01 pm

oh, meant to say, $query is the $query that you specified.

Andrew

greasypalm
New php-forum User
New php-forum User
Posts: 6
Joined: Tue May 06, 2003 11:33 am

Postby greasypalm » Wed May 07, 2003 12:06 am

Hi Andrew thanks for the help mate :)

can you please tell me if this is the right way?

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM ua_referers WHERE id='Referafriend'";
$total = 0;
$result = mysql_query($query) or die(mysql_error());
mysql_close();

while ($row = mysql_fetch_array($result))
{
$total = $total + $row["Total"];
}
echo "The total is: $total ";

thanka agian.

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed May 07, 2003 5:57 am

looks correct to me.

Regards,
Andrew

greasypalm
New php-forum User
New php-forum User
Posts: 6
Joined: Tue May 06, 2003 11:33 am

Postby greasypalm » Wed May 07, 2003 6:07 am

bezmond wrote:looks correct to me.

Regards,
Andrew


Thanks mate

User avatar
bezmond
Moderator
Moderator
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK
Contact:

Postby bezmond » Wed May 07, 2003 6:10 am

No problem, any problems just post them here or freel free to contact me privately by any of the messengers at the bottom of this post :)

Andrew


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 3 guests