Jump to content

While Loop Variable


rascle

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.