Need help with code.

Im creating a game… I have the database and everything set up to work and things are writing to it. In this case a crew business and it’s daily payout. When the business is purchased it’s posting to the database the purchase and payout like it should and setting a timer. All is well with that. But come payout time it takes a nose dive and does nothing… here is the code in question… any help would be appreciated.

if ($crew->payout <= time()){

$bus=explode("-", $crew->income);

$first = $bus[0] * 1000000;

$second = $bus[1] * 2000000;

$third = $bus[2] * 4000000;

$total = $first+$second+$third;

$total_users = mysql_num_rows(mysql_query("SELECT * FROM users WHERE crew='$crew->name'"));





$per_user=round($total / $total_users);

$add=mysql_query("SELECT * FROM users WHERE crew='$crew->name'");

while($it = mysql_fetch_object($add)){

$new_money = $it->money + $per_user;

mysql_query("UPDATE users SET money='$new_money' WHERE username='$it->username'");

$new_payout = time() + (3600*24);

mysql_query("UPDATE crews SET payout='$new_payout' WHERE name='$crew->name'");

}}

?>

First of all: have you tried debugging? Error_reporting(E_ALL), echoing variables, all that stuff? It will enable you to systematically find the error.

Secondly, I see you using the same query twice. Why?

$total_users = mysql_num_rows(mysql_query("SELECT * FROM users WHERE crew='$crew->name'")); $per_user=round($total / $total_users); $add=mysql_query("SELECT * FROM users WHERE crew='$crew->name'");

Can easily be rewritten to:

$add = mysql_query("SELECT * FROM users WHERE crew='".$crew->name."'"); $total_users = mysql_num_row($add); $per_user = round($total / $total_users);

Will ease the strain on your database when you expect heavy traffic. Also, you can easily update a table without having PHP do the calculation:

mysql_query("UPDATE users SET money = money + ".$per_user." WHERE username = '".$it->username."'");

Same goes for the crews table :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service