Jump to content

quick help.


seany123

Recommended Posts

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

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

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.