Forum Moderators: coopster

Message Too Old, No Replies

Adding Numerical Value from Rows

         

inveni0

8:19 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



I need to add all of the numbers from a set of results and display them on my page. Is this done with a do/while loop?

coopster

8:25 pm on Mar 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If this is an SQL query and it's sole purpose is to provide you a total you could just SUM() the column.

inveni0

8:33 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



I'm not familiar with that statement...do you know where I can read more about it?

coopster

8:39 pm on Mar 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is an aggregate function for use in an sql query with a GROUP BY clause. It is used like this ...
SELECT 
userName,
SUM [dev.mysql.com](payAmount) AS annualSalary
FROM weeklyPaychecks
WHERE
userName = 'Me' AND
payDate BETWEEN '2005-01-01' AND '2005-12-31'
GROUP BY userName
;

inveni0

8:45 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



I'm using mySQL:

$TotalHits = mysql_query("SELECT SUM('counter')");

When run, $TotalHits = Resource id #5

Why this error?

jatar_k

8:55 pm on Mar 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you have to extract the answer

you need to use a mysql_fetch_array or some other type function to get the actual result

inveni0

9:08 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



Okay...now I get '0' as the result...why will it not add the column?

My code now looks like:

$TotalHits = mysql_query("SELECT SUM('counter') AS 'num' FROM BnBData") or die(mysql_error());
$Hits = mysql_fetch_array($TotalHits);

I don't mean to be moving (practically) step by step, but this is quite new to me. The column I'm adding is set as an INT column. I can't figure out why it wouldn't add the values of each row together.

coopster

9:19 pm on Mar 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You have one more step.
$TotalHits = mysql_query("SELECT SUM('counter') AS 'num' FROM BnBData") or die(mysql_error()); 
$Hits = mysql_fetch_array($TotalHits);
$Hits = $Hits['num'];

mysql_fetch_array [php.net] returns an array of data. There is a good step-by-step in our PHP Forum Library [webmasterworld.com] that describes the Basics of extracting data from MySQL [webmasterworld.com].

inveni0

10:49 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



The value of $Hits['num'] is '0'. Is there something else I could be missing?

dreamcatcher

8:48 am on Mar 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does it make any difference if you remove the apostrophes?

$TotalHits = mysql_query("SELECT SUM(counter) AS num FROM BnBData") or die(mysql_error());
$Hits = mysql_fetch_array($TotalHits);

echo $Hits['num'];

dc

Habtom

1:56 pm on Mar 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to put it the echo $Hits['num']; as echo $Hits[0];

It just removes a few complications.

inveni0

1:40 am on Mar 6, 2006 (gmt 0)

10+ Year Member



If I print the array, I get:

[0] => 0 [num] => 0

The code I'm using to sum the column is:

$TotalHits = mysql_query("SELECT SUM('counter') AS 'num' FROM BnBData") or die(mysql_error());
$Hits = mysql_fetch_array($TotalHits);

I'm just not seeing what else I can do!

EDIT:

I just tried to remove the apostrophes, and that did it!

Thanks!