jaxdevil Posted November 17, 2008 Share Posted November 17, 2008 What I am trying to do is query my database, pull all the rows that match to a specific bill, and total the values of the 'dollar amount' column ($num_amt in the query below) to determine the total dollar amount paid towards that specific bill. I cannnot figure out how to do this. Any ideas? Here is my code: $paid_lookup = mysql_query("SELECT * FROM `check_database` WHERE `bill_code` = '$bill_code'") or die('MySQL Connection Error:'.mysql_error()); while($paid = mysql_fetch_array($paid_lookup)){ $amount_paid = $paid[num_amt]; } Thanks, SK Quote Link to comment https://forums.phpfreaks.com/topic/133105-solved-total-the-value-of-all-rows-contents-of-a-specific-column-in-a-database-query/ Share on other sites More sharing options...
dropfaith Posted November 17, 2008 Share Posted November 17, 2008 use sum you should be able to fiqure it from this example code <?php // Make a MySQL Connection $query = "SELECT type, SUM(price) FROM products GROUP BY type"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "Total ". $row['type']. " = $". $row['SUM(price)']; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133105-solved-total-the-value-of-all-rows-contents-of-a-specific-column-in-a-database-query/#findComment-692209 Share on other sites More sharing options...
premiso Posted November 17, 2008 Share Posted November 17, 2008 Non-SQL version $paid_lookup = mysql_query("SELECT * FROM `check_database` WHERE `bill_code` = '$bill_code'") or die('MySQL Connection Error:'.mysql_error()); $amount_paid = 0; while($paid = mysql_fetch_array($paid_lookup)){ $amount_paid = $amount_paid + $paid[num_amt]; } SQL Version: $paid_lookup = mysql_query("SELECT SUM(num_amt) FROM `check_database` WHERE `bill_code` = '$bill_code'") or die('MySQL Connection Error:'.mysql_error()); $amount_paid = 0; while($paid = mysql_fetch_array($paid_lookup)){ $amount_paid = $paid[num_amt]; } I am unsure if the SQL version will work, I think it will but yea. Quote Link to comment https://forums.phpfreaks.com/topic/133105-solved-total-the-value-of-all-rows-contents-of-a-specific-column-in-a-database-query/#findComment-692210 Share on other sites More sharing options...
jaxdevil Posted November 17, 2008 Author Share Posted November 17, 2008 Works perfect! Thank you! SK Quote Link to comment https://forums.phpfreaks.com/topic/133105-solved-total-the-value-of-all-rows-contents-of-a-specific-column-in-a-database-query/#findComment-692279 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.