Forum Moderators: coopster

Message Too Old, No Replies

Logic to Calculate the value of mysql field in loop

         

umerkk

5:31 pm on Dec 16, 2008 (gmt 0)

10+ Year Member



Hello All
I am Running SQL query " SELECT * FROM BIDREQUESTS WHERE PARENT = '$Parent'"

I have a mysql table with a field "AMT" which is int.

Suppose the table contains 3 records i.e

id = 50, Amt, 10
Id = 51, Amt 50
id = 52, Amt 100

Now i want a logic in php which could calculate the AMT of 3 rows. i.e 10+50+100

and then echo the Sum of all AMT.
As the above query fetch all the rows which contains PARENT value.

waiting for reply

Mahabub

5:53 pm on Dec 16, 2008 (gmt 0)

10+ Year Member



Dear umerkk,

You can do that by SQL that is:
SELECT SUM( Amt ) AS AmtTotal FROM sum BIDREQUESTS WHERE PARENT = '$Parent'"

and also there is another way, that is

$sql = " SELECT * FROM BIDREQUESTS WHERE PARENT = '$Parent'";
$sqlres = mysql_query($sql);
while ($row = mysql_fetch_assoc($sqlres)) {

$Total += $row['Amt'];
}

I think by SQL is the best option.

Thanks
Mahabub

umerkk

6:52 pm on Dec 16, 2008 (gmt 0)

10+ Year Member



The TOTAL variable returns 0. i want to display the sum not in a loop, but after the loop is finished, after loop code

Mahabub

7:05 pm on Dec 16, 2008 (gmt 0)

10+ Year Member



dear umerkk,

I think you missed somethings. the code and sql which i posted i double checked that it is okay. can u post table structure and some dummy data.

Thanks
Mahabub

umerkk

7:09 pm on Dec 16, 2008 (gmt 0)

10+ Year Member



Yeah, i figured it out now..
Thanks Mahbub, I appreciate your work and time
thank you so much