Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    77
    Thanks
    13
    Thanked 1 Time in 1 Post

    Getting the sum of a the value in multiple mysq rows with PHP

    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:

    PHP Code:
    $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

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Virginia
    Posts
    112
    Thanks
    11
    Thanked 7 Times in 7 Posts
    PHP Code:
    $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;

    Last edited by Afro_Programmer; 12-05-2010 at 06:15 AM. Reason: typo ;o

  • #3
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    $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']; 
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    zachatk (12-05-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •