cdoyle Posted March 17, 2008 Share Posted March 17, 2008 Hi, I have a update query, that is run with a cron every 15 minutes. $query = $db->execute("update `players` set hp = hp + 10" ); What it does is increases a players hp 10 every 15 minutes. this part is working good, but here is the problem. Depending on the level a player is, they have a maxhp (there is a field name in the players table called maxhp) How can I make it so it updates up to the maxhp and then stop? Right now, it just keeps going and going and going (lol) Link to comment https://forums.phpfreaks.com/topic/96553-need-help-with-update-query/ Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 you could query the players first. I don't know your query functions so will use basic php <?php $query = "SELECT `hp, `maxhp` FROM `players`"; $result = mysql_query($query) or die(mysql_error()); while($r = mysql_fetch_assoc($result)){ if($r['hp'] < $r['maxhp']){ mysql_query("UPDATE `players` SET `hp` = `hp` + 10"); } } Ray Link to comment https://forums.phpfreaks.com/topic/96553-need-help-with-update-query/#findComment-494105 Share on other sites More sharing options...
Psycho Posted March 17, 2008 Share Posted March 17, 2008 I'd use this one liner with an IF statement: $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" ); Link to comment https://forums.phpfreaks.com/topic/96553-need-help-with-update-query/#findComment-494118 Share on other sites More sharing options...
cdoyle Posted March 17, 2008 Author Share Posted March 17, 2008 I'd use this one liner with an IF statement: $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" ); That worked great!! Thank You Link to comment https://forums.phpfreaks.com/topic/96553-need-help-with-update-query/#findComment-494410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.