-1

first time here. I have found many useful stuff from this site, but im stuck right now.

I need help with the following code. I want this code to check every ID stats[population, populationgrowth], do the calculation which you can find in IF function and then insert result which is populationgrowth for each ID. It's a cronjob.

$get_users = mysql_query("SELECT * FROM stats") or die(mysql_error());
while($user = mysql_fetch_assoc($get_users)){

if($stats['population'] > $stats['maxpopulation']) {    
    $populationgrowth = 0;
}
else{
    $populationgrowth = 1 * ($unit['moonhut']);
}

$update = mysql_query("UPDATE stats SET populationgrowth='".$populationgrowth."' WHERE id='".$user['id']."'") or die(mysql_error());
}
Xeuq
  • 3
  • 2

1 Answers1

2

Do the arithmetic in the SQL query:

UPDATE stats s    
    SET populationgrowth = (CASE WHEN population > maxpopulation THEN 0
                                 ELSE 1 * $unit['moonhut']
                            END):

Notes:

  • 1 * is redundant.
  • Stop using mysql_. It is no longer supported.
  • Then, use parameters in your queries for safer queries.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786