1

我有一个通过 cron 运行的 php 文件。为了测试,我将它设置为每小时运行一次,但通常它会每天运行一次。我想要做的是,如果玩家将 autotechrefinery 字段设置为 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'"); }
4

1 回答 1

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;
于 2012-12-22T12:12:17.083 回答