rascle Posted April 13, 2010 Share Posted April 13, 2010 Hi i am have a load of numbers in a database and want to add them all together, so i have created a simple while loop that should add them all together, however i am not getting any results. Here is the code: <?php $totalgamespace = 0; $rhysgamespace = mysql_query("SELECT * FROM 'games'"); while($gamespace = mysql_fetch_array($rhysgamespace)){ $rgamespace = $gamespace['size']; $totalgamespace = $rgamespace + $totalgamespace; } echo $totalgamespace; ?> Please help, thanks Rhys Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 13, 2010 Share Posted April 13, 2010 Your query is failing because the table name is enclosed in single-quotes, making it a string rather than a table name. Once you fix that error, you do know that you can use the mysql SUM() function directly in your query to do this. No slow loop using parsed, tokenized, interpreted php code is needed. Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/#findComment-1041266 Share on other sites More sharing options...
rascle Posted April 13, 2010 Author Share Posted April 13, 2010 You mean: $rhysgamespace = mysql_query("SELECT * FROM \"games\""); ?? cause i tried that but it didnt work and i will have a look for the SUM function, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/#findComment-1041268 Share on other sites More sharing options...
simonrs Posted April 13, 2010 Share Posted April 13, 2010 No I believe he means: SELECT SUM(`column`) FROM `tablename` I believe that's correct. Note the use of: `, and not '. Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/#findComment-1041272 Share on other sites More sharing options...
rascle Posted April 13, 2010 Author Share Posted April 13, 2010 Thanks I changed it to: $rhysgamespace = mysql_query("SELECT SUM(`size`) FROM `games`"); $gamespace = mysql_fetch_array($rhysgamespace); echo $gamespace; But it now says ARRAY when echoed. Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/#findComment-1041276 Share on other sites More sharing options...
simonrs Posted April 13, 2010 Share Posted April 13, 2010 Have a look by doing print_r($gamespace); - see if you can work out for yourself how to get the sum out Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/#findComment-1041325 Share on other sites More sharing options...
rascle Posted April 14, 2010 Author Share Posted April 14, 2010 I used an alias and managed to get it to work: $spacegame = "SELECT SUM(size) AS gamesize FROM `games`"; $gamespace = mysql_query($spacegame); $gamespace = mysql_fetch_array($gamespace); echo $gamespace['gamesize']; Quote Link to comment https://forums.phpfreaks.com/topic/198435-while-loop-variable/#findComment-1041595 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.