help needed for calculation coding.

hi there…let me explain… first i type all my expenses in database for day one…and its goes for to day two and so on…how i do the calculation…how do i call the data and do the coding…?..please help me…

[php]
$sql=“select expenses from tbl”;
$result=mysql_query($sql)or die(mysql_error());
while($r=mysql_fetch_assoc($result))
{
$expenses=$r[‘expenses’];
$total=$expenses+$total;
}
[/php]
i try this coding but doesn’t help…no display at all…please help me…thanks in advanced

[php]
$sql = “select expenses from tbl”;
$result = mysql_query($sql) or die(mysql_error());
while($r = mysql_fetch_assoc($result))
{
$expenses = $r[‘expenses’];
$total = $expenses + $total;
}
[/php]

What I’m thinking you need, is the following:

[php]
$total = 0;
$sql = “select expenses from tbl”;
$result = mysql_query($sql) or die(mysql_error());
while($r = mysql_fetch_array($result))
{
$expenses = $r[‘expenses’];
$total += $expenses;
}
[/php]

Or perhaps even better:

[php]
$sql = “select SUM(expenses) AS total from tbl”;
$result = mysql_query($sql) or die(mysql_error());
$r = mysql_fetch_array($result);
$total = $r[‘total’];
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service