Jump to content

Updating every row


marcus

Recommended Posts

I'm making a game, and I want to update ever users points count and add 20,000 points to it.

[code]
<?php
require('../connect.php');
$sql = "SELECT * FROM `users`";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$points = $row[points];
echo $points;
?>
[/code]

When I echo $points it only shows the first row. Would it just be simply using a while statement to do this or could I just update like this?
Link to comment
Share on other sites

<?php
require('../connect.php');
$sql = "SELECT * FROM `users`";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
    $points = $row[points];
    echo $row[userID]."<br>";
    echo "  Old Points: ".$points;
    echo "<br>  New Points: ".($points+20000);
    $sql = "UPDATE `users` SET points = ".($points+20000)." WHERE userID = ".$row[userID];
    mysql_query($sql);
}
?>
Link to comment
Share on other sites

or just

$sql = "UPDATE `users` SET `points` = `points` + 20000";
    mysql_query($sql);

the above may fail as you may be restricted to teh number of queries you can run in one script - this query will do the lot all at once.

To show them all you need
[code]
<?php
require('../connect.php');
$sql = "SELECT * FROM `users`";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo $row[points] . "<br />";
}
?>
[/code]

That while statement is a prime candidate for putting all that info into a table - that way you can see the username etc. etc. (you will have to echo out each field in teh table - not just teh points).
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.