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.
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
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