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)); Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
seany123 Posted November 1, 2008 Author Share Posted November 1, 2008 what do you mean exactly? Quote Link to comment 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)); Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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` Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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` Quote Link to comment 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()); } Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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` ?> Quote Link to comment 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` Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 Strange. Paste your code again. Quote Link to comment 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))" ) } ?> Quote Link to comment 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] 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.