Good dat 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;
?>

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.

Try resetting the variable $query_recpayment to the following:

$query_recpayment = "SELECT `amountpaid`, `username`, `amountpaid` FROM `paymentsummary` WHERE `username` = '%s' AND `foryear` = '%s' AND `forlevel` ='%s'";

As you can see the end part had a long php syntax error since a variable cannot assign those extra values in the format of which you did. Also I believe there was a mysql syntax error where it says `amountpaid`. Also in addition, I added the appropriate quotes to solve any escaping string errors (mysql errors). If you need the extra values that were added at the end then please explain what they were for so I can help place them appropriately.

commented: Vey Helpful. +1

Try resetting the variable $query_recpayment to the following:

$query_recpayment = "SELECT `amountpaid`, `username`, `amountpaid` FROM `paymentsummary` WHERE `username` = '%s' AND `foryear` = '%s' AND `forlevel` ='%s'";

As you can see the end part had a long php syntax error since a variable cannot assign those extra values in the format of which you did. Also I believe there was a mysql syntax error where it says `amountpaid`. Also in addition, I added the appropriate quotes to solve any escaping string errors (mysql errors). If you need the extra values that were added at the end then please explain what they were for so I can help place them appropriately.

sir good eve.!Ive changed the sql this but the foryear returns no value:I dont kvow why?

$query_recpayment=sprintf("SELECT * FROM paymentsummary WHERE 'username'='%s' AND 'foryear'='%s' AND 'forlevel'='%s'", GetSQLValueString($_SESSION['MM_Username'], "text"), GetSQLValueString($_SESSION['MM_yearlynow'], "text"), GetSQLValueString($_SESSION['MM_yearlevels'], "text"));

$recpayment = mysql_query($query_recpayment, $enamysqldb) or die(mysql_error());
$row_recpayment = mysql_fetch_assoc($recpayment);
$totalRows_recpayment = mysql_num_rows($recpayment);

// echo $query_recpayment;

$totalpayment=0;
while($row = mysql_fetch_array($recpayment)){
	$totalpayment = $totalpayment + $row['amountpaid'];
}
$_SESSION['MM_totalbills']=$totalpayment;
echo $_SESSION['MM_totalbills']; 
?>

I have looked up a few functions in the manual and although I can't find GetValueSQLString you could probably simplify the equation to the following:

$query_recpayment="SELECT * FROM `paymentsummary` WHERE `username`='".mysql_real_escape_string($_SESSION['MM_Username'])."' AND `foryear`='".mysql_real_escape_string($_SESSION['MM_yearlynow'])."' AND `forlevel`='".mysql_real_escape_string($_SESSION['MM_yearlevels'])."'";

Hopefully that mysql query will do the trick. Also note I used an apostrophie surrounding the column names and not comas. Very important to note the difference. The Apostrophie key is found to the left of the number 1 key on the keyboard.

The reason why I prefer to use apostrophies is when sharing open source code like on these forums, there are some servers that are really picky like one I use to be on which force you to use the apostrophie. So the reason why my preference is to use the apostrophie is to garantee compatability in those rare situations like I have been in before.

I have looked up a few functions in the manual and although I can't find GetValueSQLString you could probably simplify the equation to the following:

$query_recpayment="SELECT * FROM `paymentsummary` WHERE `username`='".mysql_real_escape_string($_SESSION['MM_Username'])."' AND `foryear`='".mysql_real_escape_string($_SESSION['MM_yearlynow'])."' AND `forlevel`='".mysql_real_escape_string($_SESSION['MM_yearlevels'])."'";

Hopefully that mysql query will do the trick. Also note I used an apostrophie surrounding the column names and not comas. Very important to note the difference. The Apostrophie key is found to the left of the number 1 key on the keyboard.

The reason why I prefer to use apostrophies is when sharing open source code like on these forums, there are some servers that are really picky like one I use to be on which force you to use the apostrophie. So the reason why my preference is to use the apostrophie is to garantee compatability in those rare situations like I have been in before.

Thank you sir. I have try the trick and his helps me soso much.This problem was solve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.