Looping data and sum it up error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blocker
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 10:03 pm

Looping data and sum it up error

Post 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.
Last edited by blocker on Tue Feb 17, 2009 10:30 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Looping data and sum it up error

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Looping data and sum it up error

Post by John Cartwright »

Can you post your exact error message?
blocker
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 10:03 pm

Re: Looping data and sum it up error

Post 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;
?>
 
Last edited by blocker on Tue Feb 17, 2009 11:48 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Looping data and sum it up error

Post by John Cartwright »

Again, can you please post the exact error you are receiving.

Secondly, you can try doing

Code: Select all

echo $query_recpayment;
to see what you are actually sending at the query (for debugging).
blocker
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 10:03 pm

Re: Looping data and sum it up error

Post 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 ','
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Looping data and sum it up error

Post by susrisha »

Code: Select all

 
 $totalpayment = $totalpayment + &row['SUM(amountpaid)'];
 
you have an &row to be replaced by $row
blocker
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 10:03 pm

Re: Looping data and sum it up error

Post 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
Post Reply