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
https://forums.phpfreaks.com/topic/33011-updating-every-row/
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
https://forums.phpfreaks.com/topic/33011-updating-every-row/#findComment-153712
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
https://forums.phpfreaks.com/topic/33011-updating-every-row/#findComment-153717
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.