u214 Posted June 6, 2011 Share Posted June 6, 2011 Hello guys. Let's say I have my DB set up as: "SELECT * FROM playerinfo WHERE kills > 0" Playerinfo has information about players, each player has kills. How to add up ALL the kills FROM ALL players? Thanks. Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/ Share on other sites More sharing options...
HDFilmMaker2112 Posted June 6, 2011 Share Posted June 6, 2011 Try adding SUM(kills) to the SQL query. Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/#findComment-1225717 Share on other sites More sharing options...
mikesta707 Posted June 6, 2011 Share Posted June 6, 2011 What you are looking for is the SUM mysql function. For example $res = mysql_query("SELECT SUM(kills) as num_kills FROM playerinfo WHERE kills > 0"); $row = mysql_fetch_array($res); echo "There are : ". $row['num_kills']." total kills"; EDIT: bah sniped, but ill post anyways, as I have an example Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/#findComment-1225718 Share on other sites More sharing options...
u214 Posted June 6, 2011 Author Share Posted June 6, 2011 Hello. Thanks for the feedback. I try doing it this way: $total_kills = mysql_query("SELECT SUM(kills) FROM playerinfo", $connect); echo $total_kills; That outputs: "Resource id #5" Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/#findComment-1225722 Share on other sites More sharing options...
revraz Posted June 6, 2011 Share Posted June 6, 2011 That's because it's a resource and not the result. Use the examples that were provided. Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/#findComment-1225724 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 6, 2011 Share Posted June 6, 2011 Hello. Thanks for the feedback. I try doing it this way: $total_kills = mysql_query("SELECT SUM(kills) FROM playerinfo", $connect); echo $total_kills; That outputs: "Resource id #5" mysql_query only gives you the id location in the database. You have to apply that to an array to actually pull data out of it. Use: $total_kills=mysql_fetch_array($total_kills) directly after $total_kills = mysql_query("SELECT SUM(kills) FROM playerinfo", $connect); Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/#findComment-1225725 Share on other sites More sharing options...
u214 Posted June 6, 2011 Author Share Posted June 6, 2011 Thank you so much. Link to comment https://forums.phpfreaks.com/topic/238520-adding-all-the-results-from-all-the-fields/#findComment-1225726 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.