Jump to content

Cron not updating


Phpfr3ak

Recommended Posts

Sorry maybe a stupid question but Ive made a cron and its not updating the players cash as it should, can anyone spot why? I've looked but just cannot see it, Thanks appreciate your time for looking.

 

<?php
include("server.php");
$sql = "SELECT * FROM players";
$que = mysql_query($sql);
while($res=mysql_fetch_array($que)) {
    $sql2 = "SELECT * FROM players WHERE id = '$res[id]'";
    $que2 = mysql_query($sql2) or die(mysql_error());
    $res2 = mysql_fetch_array($que2);
    $hoes = $res2['Hoes'];
    $hobedding = $res2['MaxHoes'];
    $incomemod = $res2['HoIncome'];
    $morale = $res2['HoesMorale'];
if ($hoes < $hobedding) {
$income = (250 * $hoes * $incomemod * $morale);
} else {
if ($hoes > $hobedding) {
$income = (250 * $hobedding * $incomemod * $morale) + (125 * ($hoes - $hobedding) * $incomemod * $morale); 
}
$sql5 = "UPDATE players SET cash = cash + '$income' WHERE id = '$res[id]'";
mysql_query($sql5) or die(mysql_error()); 	
}
}
?>					

Link to comment
https://forums.phpfreaks.com/topic/256396-cron-not-updating/
Share on other sites

If you indent your code properly, you can probably see that you are only performing the UPDATE query for one of the logic conditions. Also, your if(){}else{} if logic does not cover the case where $hoes is equal to $hobedding.

 

You are also needlessly re-executing the select query inside of the loop.

 

The following should be (untested) equivalent to what you are trying to do (I renamed variables to match their meaning in the table) -

 

<?php
include "server.php";
$sql = "SELECT * FROM players";
$que = mysql_query($sql);
while($res=mysql_fetch_array($que)) {
    $hoes = $res['Hoes'];
    $maxhoes = $res['MaxHoes'];
    $hoimcome = $res['HoIncome'];
    $hoesmorale = $res['HoesMorale'];
if ($hoes <= $maxhoes) {
	$income = (250 * $hoes * $hoincome * $hoesmorale);
} else {
	$income = (250 * $maxhoes * $hoincome * $hoesmorale) + (125 * ($hoes - $maxhoes) * $hoincome * $hoesmorale);
}
$sql5 = "UPDATE players SET cash = cash + $income WHERE id = {$res['id']}";
mysql_query($sql5) or die(mysql_error()); 	
}
?>

 

And you are aware that mysql can perform logic in a query. The following (untested) is all you really need to do -

 

<?php
include "server.php";
$sql5 = "UPDATE players SET cash = cash + IF(Hoes <= MaxHoes,250*Hoes*HoIncome*HoesMorale,(250*Hoes*HoIncome*HoesMorale) + (125*(Hoes-MaxHoes)*HoIncome*HoesMorale))";	
mysql_query($sql5) or die(mysql_error());
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/256396-cron-not-updating/#findComment-1314455
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.