count total of ....

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

count total of ....

Post by blacksnday »

How can I count total value of a certain row in a field?

Table: Test
Field: access attempts

where row for access attempts would be any number from 0 - 1000
and I need to count total value of all rows with a value in access attempts
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

SELECT SUM(access attempts) AS total_attempts FROM Test
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can use either array_sum() or you can loop through the result set and add each value by concatenating.

Code: Select all

<?php
while ( $row = mysql_fetch_array($result) )
{
    $row_total += $row['access_attempts'];
}
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Thanks for the advice from both of ya's :)
Post Reply