DrTrans Posted April 14, 2010 Share Posted April 14, 2010 Property Amount Test 100 Test 300 Test 500 I want to be able to add up all fields of Amount By Property so it gives me a value of ex. 900. my code $query9 = "SELECT SUM(Amount) FROM gldetail Where Property_ID = '$prop'"; $result9 = mysql_query($query9); echo"$result9"; $sum = mysql_fetch_assoc($result9); $sum2 = $sum['Amount']; $sum3 = number_format($sum2,2); Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/ Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Change $query9 = "SELECT SUM(Amount) FROM gldetail Where Property_ID = '$prop'" To $query9 = "SELECT SUM(Amount) as amount FROM gldetail Where Property_ID = '$prop'" Then change $sum2 = $sum['Amount']; To $sum2 = $sum['amount']; Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041786 Share on other sites More sharing options...
DrTrans Posted April 14, 2010 Author Share Posted April 14, 2010 I keep getting a value of "0" i dont know why there are tons of amounts in my database some of them with "-" next to the first number, so amount could be -357.00" could this be why im getting a "0" Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041792 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Is the column set to type int? Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041795 Share on other sites More sharing options...
DrTrans Posted April 14, 2010 Author Share Posted April 14, 2010 Negative. its set to "float" Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041799 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 After $query9: echo $query9; Copy and paste that here please. Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041801 Share on other sites More sharing options...
DrTrans Posted April 14, 2010 Author Share Posted April 14, 2010 Resource ID: #3 is the responce. Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041805 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 o.O ... what? Paste your updated code here please. Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041807 Share on other sites More sharing options...
DrTrans Posted April 14, 2010 Author Share Posted April 14, 2010 $connect = mysql_connect("localhost","root",""); mysql_select_db("tenantpro"); $rentid ="6"; $query9 = "SELECT SUM(Amount) as amount FROM gldetail WHERE Property_ID = '$prop'"; echo "$query9"; $result9 = mysql_query($query9); echo "$result9"; $sum = mysql_fetch_assoc($result9); $sum2 = $sum['amount']; Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041810 Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2010 Share Posted April 14, 2010 You already have an existing thread for this problem, where someone already suggested echoing the query statement and executing that query directly against your database. For the code you have shown, echoing the $query9 variable cannot give the result you just posted. What is your real code? And the answer to your problem is that either your query is failing due to an error in the query or with the database server or your WHERE clause in the query is matching no rows. Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041812 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Remove the echo "$result9"; line please and paste what my echo $query9; outputs to the screen. Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041819 Share on other sites More sharing options...
DrTrans Posted April 14, 2010 Author Share Posted April 14, 2010 $prop = $_GET['prop']; $connect = mysql_connect("localhost","root",""); mysql_select_db("tenantpro"); $rentid ="6"; $query9 = "SELECT SUM(Amount) as amount FROM gldetail WHERE Property_ID = '$prop'"; echo $query9; $result9 = mysql_query($query9); $sum = mysql_fetch_assoc($result9); $sum2 = $sum['amount']; Responce: SELECT SUM(Amount) as amount FROM gldetail WHERE Property_ID = '435' Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041827 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 From the table, looks like it should add up to 0. :-\ Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041831 Share on other sites More sharing options...
DrTrans Posted April 14, 2010 Author Share Posted April 14, 2010 thats only partial. though Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041837 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 And when you echo $sum2; you get 0? Link to comment https://forums.phpfreaks.com/topic/198533-sum-of-table-to-get-value/#findComment-1041838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.