bajangerry Posted August 13, 2009 Share Posted August 13, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/170144-having-trouble-with-the-sum-of-selected-rows-in-table/ Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 Nothing wrong in that code. Do you have 'day' and 'month' columns separately in database or? Quote Link to comment https://forums.phpfreaks.com/topic/170144-having-trouble-with-the-sum-of-selected-rows-in-table/#findComment-897492 Share on other sites More sharing options...
mikesta707 Posted August 13, 2009 Share Posted August 13, 2009 $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! Quote Link to comment https://forums.phpfreaks.com/topic/170144-having-trouble-with-the-sum-of-selected-rows-in-table/#findComment-897498 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 You are right Quote Link to comment https://forums.phpfreaks.com/topic/170144-having-trouble-with-the-sum-of-selected-rows-in-table/#findComment-897501 Share on other sites More sharing options...
bajangerry Posted August 13, 2009 Author Share Posted August 13, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/170144-having-trouble-with-the-sum-of-selected-rows-in-table/#findComment-897525 Share on other sites More sharing options...
bajangerry Posted August 14, 2009 Author Share Posted August 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170144-having-trouble-with-the-sum-of-selected-rows-in-table/#findComment-898089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.