我有一个我通过cron运行的php文件。为了进行测试,我将其设置为每小时运行一次,但通常每天运行一次。我想要的是,如果玩家将自动细化场设置为1,那么他们应该从桌子中抽出他们的人口,他们有多少钱,研究用品和技术。
从那里,我想要的是扣除500项研究补贴,增加50项技术,并从资金中扣除10%的人口。当我手动运行文件时,所有这些似乎都有效,但是当我让它通过cron自动运行时,有些东西无法正常工作,因此它将技术设置为50,研究供应为-500,资金为0
$refCheck = mysql_query("SELECT * FROM players WHERE autotechrefinery='1'");
while($rC = mysql_fetch_array($refCheck)) {
$nation = $rC['nation'];
$pop = $rC['population'];
$rsupplies = $rC['researchsupplies'];
$cash = $rC['money'];
$tech = $rC['technology'];
$newtech = $tech+50;
$newmoney = $cash-($pop*.1);
$newsupplies = $rsupplies-500;
mysql_query("UPDATE players SET money='$newmoney', technology='$newtech', researchsupplies='$newsupplies', techbought='1' WHERE autotechrefinery='1'"); }
答案 0 :(得分:2)
代码中的错误是:UPDATE将始终更新所有条目。因此,所有条目都会获得循环最后一次传递的值。
要修复代码,您需要一些ID来标识行并在更新中使用它:
$refCheck = mysql_query("SELECT * FROM players WHERE autotechrefinery='1'");
while($rC = mysql_fetch_array($refCheck)) {
$ID = $rC['id'];
$nation = $rC['nation'];
$pop = $rC['population'];
$rsupplies = $rC['researchsupplies'];
$cash = $rC['money'];
$tech = $rC['technology'];
$newtech = $tech+50;
$newmoney = $cash-($pop*.1);
$newsupplies = $rsupplies-500;
mysql_query("UPDATE players SET money='$newmoney', technology='$newtech', researchsupplies='$newsupplies', techbought='1' WHERE id="$ID');
}
您没有要求改进,但您可以使用一个SQL语句获得相同的结果:
UPDATE players set money=money - population * .1, technology = technology + 50, researchsupplies = researchsupplies - 500;