General Doom Posted May 1, 2010 Share Posted May 1, 2010 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? Quote Link to comment Share on other sites More sharing options...
Danny696 Posted May 1, 2010 Share Posted May 1, 2010 Use a while loop Quote Link to comment Share on other sites More sharing options...
General Doom Posted May 2, 2010 Author Share Posted May 2, 2010 Can't believe I didn't think of that. Thankyou very much! Quote Link to comment Share on other sites More sharing options...
CrazyT Posted May 2, 2010 Share Posted May 2, 2010 Watch out how you use that while loop, say if you have 200 members on the while loop you will be running 200 queries. :) Quote Link to comment Share on other sites More sharing options...
Zeggy Posted May 2, 2010 Share Posted May 2, 2010 Try this instead: <?php mysql_query("UPDATE users SET credits=credits+economy"); ?> Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted May 6, 2010 Share Posted May 6, 2010 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.')'); Quote Link to comment Share on other sites More sharing options...
General Doom Posted May 12, 2010 Author Share Posted May 12, 2010 Thanks for the reply's everyone, in the end I used zeggys simple script and it worked a treat for all my members! Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted May 12, 2010 Share Posted May 12, 2010 Your welcome pal, sorry if my solution was confusing. Quote Link to comment Share on other sites More sharing options...
General Doom Posted May 13, 2010 Author Share Posted May 13, 2010 Yeach cheers, wasn't confusing as such, just seemed more efficient to do it the other way. Quote Link to comment Share on other sites More sharing options...
Zero-Affect Posted May 13, 2010 Share Posted May 13, 2010 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.')'); Quote Link to comment 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.