Jump to content
MakeWebGames

PHP code help


General Doom

Recommended Posts

Hello all.

 

Im trying to create a PHP code that updates all users money every hour based on there income.

Im doing this using a crontab so that it runs on exactly every hour.

 

However, every users current money and income is different, so, is there a way to get the mySQL to cycle through every user without having to run the script for multiple users?

 

The surrent script im using only does it for the 1st player registered (me), any ideas?

 

 

<?php
$playerid=mysql_query("SELECT * FROM users");
$user=mysql_fetch_object($playerid);
$newcreds=$user->credits+$user->economy;
$userid=$user->id;
mysql_query("UPDATE users SET credits='$newcreds' WHERE id='$userid'");
?>

 

 

Any ideas on how I can get it to do this for every user?

Link to comment
Share on other sites

but for example you have a load of members you need to update but they have nothing in common you could do something like

 

          # If your wanting to use an array it's a little different
/*          $mem_array = array(1,3,6,8,9,12,172,1829,2991);
         $mem_val = '';
         $mem_cnt = 1;
foreach ( $mem_array as $value ) {
         $mem_val .= $value.''.(($mem_cnt < count($mem_array)) ? ',' : '' ).'';
         $mem_cnt++;
}
         $members = $mem_val;*/
         # End array method
         $members = '1,3,6,8,9,12,172,1829,2991';
         $updated_creds = 100;
    mysql_query('UPDATE `members` SET `credits` = `credits` + '.$updated_creds.' WHERE `id` IN('.$members.')');
Link to comment
Share on other sites

Yeah it was if your updating all members i was making a example of if you wanted to only update specific users which had nothing in the db in common.

for example

    $num_what = 0;
    $array_peeps = array(1,3,54,68,99,192);
foreach ($arr as $val) {
    mysql_query('UPDATE `users` SET `money` = `money` + 100 WHERE `userid` = '.$val);
}
          # INSTEAD DO:
    mysql_query('UPDATE `users` SET `money` = `money` + 100 WHERE `userid` IN('.$array_peeps.')');
Link to comment
Share on other sites

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.   Paste as plain text instead

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