Page 1 of 1
Looping data and sum it up error
Posted: Tue Feb 17, 2009 10:09 pm
by blocker
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:
Code: Select all
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.
Re: Looping data and sum it up error
Posted: Tue Feb 17, 2009 10:24 pm
by Benjamin
Please use the appropriate
Code: Select all
[ /code] tags when posting code blocks in the forums. Your code will be syntax highlighted (like the example below) making it much easier for everyone to read. You will most likely receive more answers too!
Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces. You can even start right now by editing your existing post!
If you are new to the forums, please be sure to read:
[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]
If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online. You'll find code samples, detailed documentation, comments and more.
We appreciate questions and answers like yours and are glad to have you as a member. Thank you for contributing to phpDN!
Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
Re: Looping data and sum it up error
Posted: Tue Feb 17, 2009 11:41 pm
by John Cartwright
Can you post your exact error message?
Re: Looping data and sum it up error
Posted: Tue Feb 17, 2009 11:46 pm
by blocker
my query above doesnt run. maybe there something lucking character in my sql.pls help
Thank you and God bless.
Code: Select all
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;
?>
Re: Looping data and sum it up error
Posted: Tue Feb 17, 2009 11:48 pm
by John Cartwright
Again, can you please post the exact error you are receiving.
Secondly, you can try doing
to see what you are actually sending at the query (for debugging).
Re: Looping data and sum it up error
Posted: Tue Feb 17, 2009 11:53 pm
by blocker
the error says un expected character on the query line.but i cant find something wrong from it.
Code: Select all
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;
?>
syntax error, unexpected ','
Re: Looping data and sum it up error
Posted: Wed Feb 18, 2009 12:19 am
by susrisha
Code: Select all
$totalpayment = $totalpayment + &row['SUM(amountpaid)'];
you have an &row to be replaced by $row
Re: Looping data and sum it up error
Posted: Wed Feb 18, 2009 3:18 am
by blocker
the error says that unexpected ',' this the below code:
Code: Select all
$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']));
Whats wrong the the query.?pls help