ririe44 Posted March 3, 2009 Share Posted March 3, 2009 I'm having a hard time getting any good results by asking what I'm asking... because everyone has already asked how to "add" a row or field to a "database". So, anyway... How do I go about adding all the fields in a certain column of my database? It's obviously money, so I have: table name=amounts categoryamount cat156.85 cat2101.94 total?? ?? ?? Any help will be awesome, thanks! Link to comment https://forums.phpfreaks.com/topic/147779-math-add-fields-of-database/ Share on other sites More sharing options...
Maq Posted March 3, 2009 Share Posted March 3, 2009 SELECT SUM(amount) FROM amounts; Link to comment https://forums.phpfreaks.com/topic/147779-math-add-fields-of-database/#findComment-775711 Share on other sites More sharing options...
ririe44 Posted March 3, 2009 Author Share Posted March 3, 2009 hmm... okay, this is what I've put into my code... $exp_total = mysql_query("SELECT SUM(amount) FROM $tbl_name") or die(mysql_error()); Then, where I display this: Print "<td>Total Expenses</td> <td colspan=2 align=right>".$exp_total."</td>"; My result says "Resource id #4", where I was expecting a number... any thoughts? Link to comment https://forums.phpfreaks.com/topic/147779-math-add-fields-of-database/#findComment-775730 Share on other sites More sharing options...
Mchl Posted March 3, 2009 Share Posted March 3, 2009 $exp_total is a Result resource. You have to use mysql_fetch_array to get data from it. Link to comment https://forums.phpfreaks.com/topic/147779-math-add-fields-of-database/#findComment-775732 Share on other sites More sharing options...
Maq Posted March 3, 2009 Share Posted March 3, 2009 Try: $exp_total = "SELECT SUM(amount) FROM $tbl_name"; $result = mysql_query($exp_total) or die(msyql_error()); $row = mysql_fetch_assoc($result); Print "Total Expenses ".$row['SUM(amount)'].""; Link to comment https://forums.phpfreaks.com/topic/147779-math-add-fields-of-database/#findComment-775737 Share on other sites More sharing options...
ririe44 Posted March 3, 2009 Author Share Posted March 3, 2009 Awesome... that's it! Thanks! Link to comment https://forums.phpfreaks.com/topic/147779-math-add-fields-of-database/#findComment-775742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.