Phpfr3ak Posted February 4, 2012 Share Posted February 4, 2012 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()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256396-cron-not-updating/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2012 Share Posted February 4, 2012 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256396-cron-not-updating/#findComment-1314455 Share on other sites More sharing options...
trq Posted February 5, 2012 Share Posted February 5, 2012 ps: Your script is not a "cron". While it might be executed by cron, it has nothing to do with cron. Quote Link to comment https://forums.phpfreaks.com/topic/256396-cron-not-updating/#findComment-1314671 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.