seany123 Posted November 1, 2008 Share Posted November 1, 2008 in my database i have value named awake and another named maxawake. this code should basically make "awake" + 5 until it reaches "maxawake" but for some reason it isnt doing it.. can someone tell me if ive done something wrong? $awakeupdate = $player->awake + 5; $query = $db->execute("update `players` set `awake`=? where `id`=? and `awake`<`maxawake`", array($awakeupdate, $player->id)); Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/ Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Because you execute a query only once, it only increments 'awake' once. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679902 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 what do you mean exactly? Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679903 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 I understand, that you want to do something like this pseudocode: while (player.awake < player.maxawake) { player.awake = player.awake+5; } And your code does pseudocode: if (player.awake < player.maxawake) { player.awake = player.awake+5; } In other words, if you want it to increase player.awake several times, until it reaches player.awakemax, you need to use a loop something like that while (($player->awake + $awakeupdate) < $player->maxawake) { $awakeupdate += $player->awake + 5; } $query = $db->execute("update `players` set `awake`=? where `id`=? and `awake`<`maxawake`", array($awakeupdate, $player->id)); or you can just check, how many 5s you need to add to player.awake to get player.maxawake $multiplier = (int)(($player->maxawake - $player->awake)/5); $awakeupdate = $mulitplier * 5; $query = $db->execute("update `players` set `awake`=? where `id`=? and `awake`<`maxawake`", array($awakeupdate, $player->id)); Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679909 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 no sorry maybe i havnt made myself clear enough. The code above is in a page which is called every X amount of minutes. (its essentially working as a Cron) So i only need it to run once each times the page is called. but for some reason the "awake" value isnt increasing. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679910 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Makes more sense now. If so, you should check if query runs without errors. What database library you use? Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679912 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 well the page seems to do the other "crons" and i dont get a error page of any sort. what do you mean by database library? Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679913 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 How you connect to database? Is it mysqli? Try doing: $awakeupdate = $player->awake + 5; $query = "update `players` set `awake`= `$awakeupdate` where `id`= `$player->id` and `awake`<`maxawake`"; echo $query; And see if query doesn't have errors, and if 'where' conditions are valid. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679914 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 i tried what you said it didnt echo anything... just didnt work. EDIT: this is was i got: update `players` set `awake`= `5` where `id`= `` and `awake`<`maxawake` Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679919 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 but it still didnt add 5 to awake. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679922 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 `id`= `` As you can see, there's no id defined. Check if $player->id is not empty. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679928 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 ah i just realised something... this cron would only work for 1 person. But instead i need it to work for everyone. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679929 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 This makes it really easy... update `players` set `awake`= `awake`+5 where `awake`<`maxawake` Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679932 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 for example: this is a cron i use to gain 25% hp. $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $hp = $row['hp'] + ($row['maxhp'] * .25); $hp = ($hp > $row['maxhp']) ? $row['maxhp'] : $hp; $query = sprintf('UPDATE players SET hp = %d WHERE id = %d', $hp, $row['id']); mysql_query($query) or die(mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679935 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 The query I posted, should work for you. Did it not? Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-679943 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 no, using the code you gave means i cant actually get onto my website. page just stays white. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680139 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 How did you put it into your script? Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680143 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 <?php if(!defined('IN_GAME')) die('HACK'); $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $energy = $row['energy'] + ($row['maxenergy'] * .20); $energy = ($energy > $row['maxenergy']) ? $row['maxenergy'] : $energy; $query = sprintf('UPDATE players SET energy = %d WHERE id = %d', $energy, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $nerve = $row['nerve'] + ($row['maxnerve'] * .20); $nerve = ($nerve > $row['maxnerve']) ? $row['maxnerve'] : $nerve; $query = sprintf('UPDATE players SET nerve = %d WHERE id = %d', $nerve, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $hp = $row['hp'] + ($row['maxhp'] * .25); $hp = ($hp > $row['maxhp']) ? $row['maxhp'] : $hp; $query = sprintf('UPDATE players SET hp = %d WHERE id = %d', $hp, $row['id']); mysql_query($query) or die(mysql_error()); } <?php if(!defined('IN_GAME')) die('HACK'); $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $energy = $row['energy'] + ($row['maxenergy'] * .20); $energy = ($energy > $row['maxenergy']) ? $row['maxenergy'] : $energy; $query = sprintf('UPDATE players SET energy = %d WHERE id = %d', $energy, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $nerve = $row['nerve'] + ($row['maxnerve'] * .20); $nerve = ($nerve > $row['maxnerve']) ? $row['maxnerve'] : $nerve; $query = sprintf('UPDATE players SET nerve = %d WHERE id = %d', $nerve, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $hp = $row['hp'] + ($row['maxhp'] * .25); $hp = ($hp > $row['maxhp']) ? $row['maxhp'] : $hp; $query = sprintf('UPDATE players SET hp = %d WHERE id = %d', $hp, $row['id']); mysql_query($query) or die(mysql_error()); } update `players` set `awake`= `awake`+5 where `awake`<`maxawake` ?> Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680144 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 D'oh... That's SQL query, and you're supposed to use it just like other queries, i.e. $query = "UPDATE `players` SET `awake`= `awake`+5 WHERE `awake`<`maxawake`"; mysql_query($query) or die(mysql_error()); This should increase `awake` by 5 for all players whose `awake` is lower than their `maxawake` Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680148 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 $query = "UPDATE `players` SET `awake`= `awake`+5 WHERE `awake`<`maxawake`"; mysql_query($query) or die(mysql_error()) is that how it should be done? im sorry i didnt sleep at all last night lol. because i put that code in and still not letting me on my website. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680153 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 You're missing a semicolon. mysql_query($query) or die(mysql_error()); Put error_reporting(E_ALL); at top of your script to enable error messages that will tell you what's going wrong. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680157 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 Not getting any error reports. its just a White screen. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680166 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Strange. Paste your code again. Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680167 Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 <?php if(!defined('IN_GAME')) die('HACK'); $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $energy = $row['energy'] + ($row['maxenergy'] * .20); $energy = ($energy > $row['maxenergy']) ? $row['maxenergy'] : $energy; $query = sprintf('UPDATE players SET energy = %d WHERE id = %d', $energy, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $nerve = $row['nerve'] + ($row['maxnerve'] * .20); $nerve = ($nerve > $row['maxnerve']) ? $row['maxnerve'] : $nerve; $query = sprintf('UPDATE players SET nerve = %d WHERE id = %d', $nerve, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $hp = $row['hp'] + ($row['maxhp'] * .25); $hp = ($hp > $row['maxhp']) ? $row['maxhp'] : $hp; $query = sprintf('UPDATE players SET hp = %d WHERE id = %d', $hp, $row['id']); mysql_query($query) or die(mysql_error()); } <?php if(!defined('IN_GAME')) die('HACK'); $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $energy = $row['energy'] + ($row['maxenergy'] * .20); $energy = ($energy > $row['maxenergy']) ? $row['maxenergy'] : $energy; $query = sprintf('UPDATE players SET energy = %d WHERE id = %d', $energy, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $nerve = $row['nerve'] + ($row['maxnerve'] * .20); $nerve = ($nerve > $row['maxnerve']) ? $row['maxnerve'] : $nerve; $query = sprintf('UPDATE players SET nerve = %d WHERE id = %d', $nerve, $row['id']); mysql_query($query) or die(mysql_error()); } $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $hp = $row['hp'] + ($row['maxhp'] * .25); $hp = ($hp > $row['maxhp']) ? $row['maxhp'] : $hp; $query = sprintf('UPDATE players SET hp = %d WHERE id = %d', $hp, $row['id']); mysql_query($query) or die(mysql_error()); } $query = $db->execute("update `players` set energy = IF((maxawake + 5)>maxawake, maxawake, (energy + 10))" ) } ?> Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680183 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Did you notice that you have your script repeated in this file? Look, there's <?php if(!defined('IN_GAME')) die('HACK'); in the middle, and after that all the script is repeated from the beginning. Also you didn't put error_reporting(E_ALL) as I told you. It would give you error message on that.[/code] Link to comment https://forums.phpfreaks.com/topic/130973-quick-help/#findComment-680191 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.