tobitimac Posted April 29, 2011 Share Posted April 29, 2011 Hi Can someone help me on this.. The SUM(quantityHand) is not summing up the quantityHand if(isset($select)&&$select!=""){ // Get records from database (table "name_list"). //$result=@mysql_query("select * FROM `inventory` WHERE id='$select' GROUP BY `itemNumber` ORDER BY `itemNumber`"); $result=@mysql_query("SELECT DISTINCT itemNumber, itemDesc, quantityHand, SUM(quantityHand) AS quantityHand FROM inventory WHERE id='$select' GROUP BY `itemNumber` ORDER BY `itemNumber`"); while ($row=@mysql_fetch_assoc($result)) { echo"<b>"; echo "<tr><td style=\"font-size: 15px;\"><b>"; echo $row['itemNumber']; echo "</td><td style=\"font-size: 15px;\"><b>"; echo $row['itemDesc']; //echo "</td><td style=\"text-align: right;\"><b>"; echo "</td><td style=\"font-size: 15px; text-align: center;\"><b>"; echo $row['quantityHand']; echo"</b>"; // End While loop } // End if statement. } Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/ Share on other sites More sharing options...
mellis95 Posted April 29, 2011 Share Posted April 29, 2011 For one thing, you have the same field name used twice. Once as the actual field name, and once as the name for the SUM. Change your query to something like this and see what it looks like: $result=@mysql_query("SELECT DISTINCT itemNumber, itemDesc, quantityHand, SUM(quantityHand) AS quantityHandSUM FROM inventory WHERE id='$select' GROUP BY `itemNumber` ORDER BY `itemNumber`"); Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/#findComment-1208004 Share on other sites More sharing options...
tobitimac Posted April 29, 2011 Author Share Posted April 29, 2011 Thanks Matt, still the same problem. nothing changed. the quantityHand is still not summing up.. Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/#findComment-1208007 Share on other sites More sharing options...
mellis95 Posted April 29, 2011 Share Posted April 29, 2011 Don't SELECT DISTINCT, just SELECT. I don't have a mysql server to test on right now, but I believe if you use SELECT DISTINCT it only selects each DISTINCT itemNumber. To SUM them and group you need to select duplicate item numbers. Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/#findComment-1208009 Share on other sites More sharing options...
tobitimac Posted April 29, 2011 Author Share Posted April 29, 2011 NOTHING CHANGES.. dont why its behaving this way.. Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/#findComment-1208011 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2011 Share Posted April 29, 2011 When you changed the alias name in the query, did you change your php code to use that new name? What does show up on the web page where the quantity should be? Do you get a zero or is it blank? Have you done a 'view source' in your browser, in case the output is present but is not being rendered? Are you sure you have any rows that match WHERE id='$select', because the SUM() will return a null value if there are no rows. Try using var_dump($row['quantityHandSUM']) to see what you are actually getting. It would help if you echoed $select to make sure it has the value you expect (just because it is set and not an empty string, doesn't mean it has the value you expect) and it would help if you showed us the rows from your table that the query should be operating on. Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/#findComment-1208014 Share on other sites More sharing options...
tobitimac Posted April 29, 2011 Author Share Posted April 29, 2011 Thanks guy the problem is fixed. All i did was to change the select to be item number since am selecting by item number and i always used echo"$select"; to see what am selecting which help to see that i need to select to item number. Quote Link to comment https://forums.phpfreaks.com/topic/235053-sum-function-is-not-working/#findComment-1208221 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.