looping data in a query and summing error

Discussion in 'PHP' started by blocker, Feb 17, 2009.

  1. #1
    Good day to everyone.!

    Ive just so tired just to finished my thesis. Ive got problem in php summing up while looping and filtering. here is my code:
    
    
    mysql_select_db($database_enamysqldb, $enamysqldb);
    $query_recpayment = "SELECT amountpaid, username, SUM(amountpaid) FROM paymentsummary WHERE username = %s and foryear = %s and forlevel =%s", GetSQLValueString($colname_reclog, "text"), GetSQLValueString($_SESSION['MM_dyearnow']), GetSQLValueString($_SESSION['MM_enrolto']));
    
    $recpayment = mysql_query($query_recpayment, $enamysqldb) or die(mysql_error());
    
    $row_recpayment = mysql_fetch_assoc($recpayment);
    $totalRows_recpayment = mysql_num_rows($recpayment);
    
    $totalpayment=0;
    while($row = mysql_fetch_array($recpayment)){
    $totalpayment = $totalpayment + &row['SUM(amountpaid)'];
    }
    $_SESSION['MM_totalbills']=$totalpayment;
    ?>
    
    Code (php):
    I want to sum up all the value of the amountpaid field base on a filter of the where clause in mysql query. the sum then will be stored in a variable called $totalpayment, and after the sum is stored on that variable, i want to store it in a session variable as what the code looks above. But i think there something missing in my code. It fails to run, it results error unexpected.

    Pls help.

    Thank you. Peace on this mother earth.
     
    blocker, Feb 17, 2009 IP
  2. Techmonkey

    Techmonkey Active Member

    Messages:
    107
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Not sure why you are using the sum function in your SQL statement if your adding up manually anyways.

    Try this:

    
    mysql_select_db($database_enamysqldb, $enamysqldb);
    $query_recpayment = "SELECT amountpaid, username FROM paymentsummary WHERE username = %s and foryear = %s and forlevel =%s", GetSQLValueString($colname_reclog, "text"), GetSQLValueString($_SESSION['MM_dyearnow']), GetSQLValueString($_SESSION['MM_enrolto']));
    
    $recpayment = mysql_query($query_recpayment, $enamysqldb) or die(mysql_error());
    
    $row_recpayment = mysql_fetch_assoc($recpayment);
    $totalRows_recpayment = mysql_num_rows($recpayment);
    
    $totalpayment=0;
    while($row = mysql_fetch_array($recpayment)){
    $totalpayment = $totalpayment + $row['amountpaid'];
    }
    $_SESSION['MM_totalbills']=$totalpayment;
    ?>
    
    
    Code (markup):
    If I am completely off the mark here I noticed you have &row['SUM(amountpaid)'] instead of $row
     
    Techmonkey, Feb 18, 2009 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Maybe getmonkey gets you.

    However, I have something to correct your query.

    Regarding this line:
    
    $query_recpayment = "SELECT amountpaid, username, SUM(amountpaid) FROM paymentsummary...
    
    PHP:
    Whenever I use SUM() , I write it like this:
    
    $query_recpayment = "SELECT amountpaid, username, SUM(amountpaid) as anynameyouwant FROM paymentsummary...
    
    PHP:
    Then, you can call the sum using $row['anynameyouwant']

    :: ads2help
     
    ads2help, Feb 18, 2009 IP