Jump to content

Having trouble with the SUM of selected rows in table


bajangerry

Recommended Posts

Guys,

I am having some trouble getting the SUM of selected rows in a query. This is what I have so far:

$amt = mysql_query("SELECT SUM(amt) AS total FROM provider WHERE day = '01' AND month = '06'");

$row =mysql_fetch_array($amt);

$total = ($row->amt);

echo $total;

 

What I am trying to do is to get the SUM of the "amt" column in that table "provider" based on the day and month parameters then echo this to screen. Can anyone point out what I am doing wrong here?

  :shrug::confused:

$row is an array, not an object. So you can't use the -> symbol to choose data. also, you probably want the php to return an associative array. Also, the sum will be total, not amt (I think, i could be wrong)

 

$amt = mysql_query("SELECT SUM(amt) AS total FROM provider WHERE day = '01' AND month = '06'");
$row =mysql_fetch_assoc($amt);
$total = $row['total'];//if doesnt work try $row['amt'];
echo $total;

 

that should work. Hope that helps!

Ok, I made the change as suggested by mikesta707 and I am getting a result... gotta admit that at this early stage the result looks wrong but I have not done a manual calculation to be sure yet, will do that soon though.

 

Thanks for the input guys!

Ok, I think I need to attack this from a different direction as I am not getting what I want this way.

 

I have a while loop like this:

 

while($row1 = mysql_fetch_object($result1)) {

$query = "SELECT * FROM import WHERE accountcode ='{$row1->accountcode}' AND accountcode <>'0' AND day BETWEEN '$day'AND '$day1' AND month BETWEEN '$month' AND '$month1' AND year BETWEEN '$year' AND '$year1'";

$result = mysql_query($query);

 

This is then printed to a table on a page.

 

There is a field ($result->amt) which is a cost amount that I would like to total and show at the end of the table. Can anyone explain how to do this? Assuming it is possible!

 

Thanks guys.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.