droopie
droopie

Reputation: 441

sql sum of results

I am pulling everything from a database table and displaying specific data in php.

So far all my code is working, I'm able to display in php everything but I'm trying to make another php file to only display specific data. For example I want it to find everything in the movieview table and only display all row data that has a playcount of equal or greater than 1 and to display it by title ascending which is

 $SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
 $result = mysql_query($SQL);

 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['c11'];
 $row = mysql_fetch_assoc($run); 
 $sum = $row['value_sum'];

Now I want to echo $sum; to add up all values of c11 from all the results that matched playcount of equal or greater then 1. The reason I haven't tried the SQL sum is because below I am displaying more columns which i believe is the reason for select *

edit: if i do the following:

 $db_handle = mysql_connect($server, $username, $password);
 $db_found = mysql_select_db($database, $db_handle);

 if ($db_found) {

 $SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
 $result = mysql_query($SQL);

 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['c11'];

 echo $run;

it does display the times of all the items with a playcount of 1 or more which is good. now im just just wish to make a total of all those $run values in 1 sum and echo that. i also have extra tables that are called in the php file, just a note, like mid, idb, and c00.

now, if i do : $db_handle = mysql_connect($server, $username, $password); $db_found = mysql_select_db($database, $db_handle);

 if ($db_found) {

 $SQL = "select SUM(c11) AS totalrun from movieview WHERE playCount>=1 Order By c00 Asc ";
 $result = mysql_query($SQL);

 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['totalrun'];

i am able to echo the totals, yey!, but i cant call on the other columns listed above.

Upvotes: 0

Views: 108

Answers (3)

Ajay
Ajay

Reputation: 785

If you just keep on adding the total run it should work , If I have understood it correctly

$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

 $SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc"; 
 $result = mysql_query($SQL);
 $run=0;
 while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $run + $db_field['c11'];
 }
echo $run;

Upvotes: 1

droopie
droopie

Reputation: 441

i was able to do it by creating another sql query. not sure how proper it is but it works. the only thing missing is that each result is only counted 1x so if a playcount of 2 or more is only seen as 1. will work on that later unless you can reply with a fix :D

 while ( $db_field = mysql_fetch_array($result) ) {

 $run = $db_field['SUM(c11)'];

Upvotes: 0

IseNgaRt
IseNgaRt

Reputation: 609

If i understood well your question, you can do this:

$counter=0;

while ( $db_field = mysql_fetch_assoc($result) ) {

 $imdb = $db_field['c09'];
 $run = $db_field['c11'];
 $row = mysql_fetch_assoc($run); 
 $sum = $row['value_sum'];
 $counter .= $row['c11'];
}



echo $counter;

Upvotes: 1

Related Questions