...

View Full Version : Getting the sum of a the value in multiple mysq rows with PHP



zachatk
12-05-2010, 03:28 AM
Hey

So I got a database. I want get the RATING_TOTAL column from each occurrence of a specific username to add up. The rating total is a integer 1-10.

I tried this:



$now = $req_user_info['username'];

//CONNECT...\\

$query = "SELECT `RATING_TOTAL` FROM ACTIVE WHERE USERNAME='$now'";
$result = mysql_query($query);

for($i=0;$i<mysql_num_rows($result);$i++) {
$row=mysql_fetch_assoc($result);
}
$sub_score = $row["RATING_TOTAL"];


But that isn't adding any of them up but just retrieving all the records. When I echo $sub_score it doesn't display anything.

Not exactly sure what to do. Maybe like have $result = $query + however many queries there are.

Thanks

Afro_Programmer
12-05-2010, 04:30 AM
$now = $req_user_info['username'];

//CONNECT...\\

$query = "SELECT `RATING_TOTAL` FROM ACTIVE WHERE USERNAME='$now'";
$result = mysql_query($query);

$temp = 0;
while ( $row=mysql_fetch_assoc($result) )
{
$sub_score = $row["RATING_TOTAL"];
$temp += $sub_score;
}

Lamped
12-05-2010, 04:41 AM
$now = $req_user_info['username'];

//CONNECT...\\

$query = "SELECT SUM(`RATING_TOTAL`) AS `TOTAL` FROM ACTIVE WHERE USERNAME='$now'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$sub_score = $row['TOTAL'];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum