MasterACE14 Posted October 12, 2007 Share Posted October 12, 2007 Evening PHP Freaks, I have some code below, which is suppose to update every users 'ammo' column and 'money' column every hour(using a cronjob). The cronjob is running fine. As I have it e-mail me when it runs. But for the MySQL query to update the 2 columns, I am getting undesired results. The code I have is suppose to UPDATE every users ammo column, by working out what their current amount of ammo is, and adding 3 to it. And for the Money field, it is suppose to work out what their current amount of money is and add their hourly income. here is the cronjob script: <?php // cron job /** includes **/ include_once '/home/ace/public_html/conflictingforces/functions.php'; include_once '/home/ace/public_html/conflictingforces/weapons.php'; include_once '/home/ace/public_html/conflictingforces/armors.php'; include_once '/home/ace/public_html/conflictingforces/vehicles.php'; // --- variables needed for turn change --- // /**********************/ // Player Select Variables // /**********************/ // User Information $all_player_accountid = all_table("id"); $all_player_username = all_table("username"); $all_player_email = all_table("email"); $all_player_race = all_race(); // Military Effectiveness $all_player_strikeaction = all_table("strikeaction"); $all_player_defenceaction = all_table("defenceaction"); $all_player_covertaction = all_table("covertaction"); // General Statistics $all_player_level = all_table("level"); $all_player_currentexp = all_table("currentexp"); $all_player_neededexp = all_table("neededexp"); $all_player_skilllevel = all_table("skilllevel"); $all_player_skillpoints = all_table("skillpoints"); $all_player_strength = all_table("strength"); $all_player_agility = all_table("agility"); $all_player_intelligence = all_table("intelligence"); // Talent Statistics $all_player_talentid = all_table("talentid"); $all_player_talentname = all_table("talentname"); $all_player_talentlevel = all_table("talentlevel"); $all_player_talentpoints = all_table("talentpoints"); // Alliance Information $all_player_allianceid = all_table("allianceid"); $all_player_alliancename = all_table("alliancename"); $all_player_allianceposition = all_table("allianceposition"); // Inventory $all_player_weaponid = all_table("weaponid"); $all_player_armorid = all_table("armorid"); $all_player_vehicleid = all_table("vehicleid"); // Money $all_player_money = all_table("money"); // Ammo $all_player_ammo = all_table("ammo"); // Equipped Items // Names $equippedweaponname = select_array($weapons,$player_weaponid,"weapon"); $equippedarmorname = select_array($armors,$player_armorid,"armor"); $equippedvehiclename = select_array($vehicles,$player_vehicleid,"vehicle"); // ID $equippedweaponid = select_array($weapons,$player_weaponid,"id"); $equippedarmorid = select_array($armors,$player_armorid,"id"); $equippedvehicleid = select_array($vehicles,$player_vehicleid,"id"); // Damage, Defence and Power $equippedweapondamage = select_array($weapons,$player_weaponid,"damage"); $equippedarmordefence = select_array($armors,$player_armorid,"defence"); $equippedvehiclepower = select_array($vehicles,$player_vehicleid,"power"); /*********************** MATHS ***********************/ // Players Actions $strike_action = (($equippedweapondamage + $all_player_strength) + $equippedvehiclepower) * 100; $defence_action = (($equippedarmordefence + $all_player_agility) + $equippedvehiclepower) * 100; $covert_action = ($equippedvehiclepower + $all_player_intelligence) * 100; // Players Actions Total $total_action = (($strike_action + $defence_action) + $covert_action); // Workout the Players Income // $players_income = ((($strike_action * $defence_action) * $covert_action) / 100000000); $players_new_income = $all_player_money + $players_income; $new_ammo = $all_player_ammo + 3; // connect to the database $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); /****** Give Players their Income ******/ //**** Then do the Query ****// // SQL query to update the players account into the database $sql = "UPDATE `cf_users` SET money = '$players_income', ammo = '$new_ammo'"; // execute the query $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); /****** Give Players their Ammo ******/ // E-mail me the Cron Results // e-mail the person their password $to = "ace@crikeygames.com.au"; $subject = "Conflicting Forces Cron Job"; // message $message = 'The Cron Job ran at ' . date("H:i:s - M j, Y") . ''; // headers $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html;"; $headers .= " charset=iso-8859-1\r\n"; $headers .= "From: ace@crikeygames.com.au \r\n"; mail($to, $subject, $message, $headers); ?> and here is the 2 custom functions I have used in the cronjob: <?php function all_table($select){ $rs = mysql_connect("localhost", "ace_ACE", "*****"); mysql_select_db("ace_cf", $rs); $sql = "SELECT `" . $select . "` FROM `cf_users`"; $rs = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); while($row = mysql_fetch_assoc($rs)){ if(isset($row[$select])){return $row[$select];} } } // Select 1 field from an Array function select_array($array,$id,$field) { return $array[$id][$field]; } ?> any help is greatly appreciated Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/ Share on other sites More sharing options...
MasterACE14 Posted October 12, 2007 Author Share Posted October 12, 2007 anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-367781 Share on other sites More sharing options...
Aureole Posted October 12, 2007 Share Posted October 12, 2007 The code I have is suppose to UPDATE every users ammo column, by working out what their current amount of ammo is, and adding 3 to it. I can't really help you with your problem but for the above part... you could do something like... $query = "UPDATE `cf_users` SET money='{$players_income}', ammo=ammo+3 "; Right now you are doing something to work out the user's new ammo when you don't really have to... $sql = "UPDATE `cf_users` SET money = '$players_income', ammo = '$new_ammo'"; Just trying to make it simpler for you. Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-367784 Share on other sites More sharing options...
MasterACE14 Posted October 12, 2007 Author Share Posted October 12, 2007 ok, that has got the ammo column being updated correctly for each user. But I am still having no success with the users 'income'. :-\ when I ran the cronjob, it updated every users money to '10'. But no matter how many times I run the cronjob now it doesn't change the money column for any users. Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-367791 Share on other sites More sharing options...
Aureole Posted October 12, 2007 Share Posted October 12, 2007 If you're not getting any errors I would assume it's a typo somewhere... scour through your code looking for typos. Anyway if that got the ammo count working then there is no doubt a problem with the way you are working out $players_income. I'd have a look for you but I have to go to the shop real quick. Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-367805 Share on other sites More sharing options...
Cagecrawler Posted October 12, 2007 Share Posted October 12, 2007 In the UPDATE, $player_income should be $players_new_income, no? Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-367807 Share on other sites More sharing options...
MasterACE14 Posted October 13, 2007 Author Share Posted October 13, 2007 In the UPDATE, $player_income should be $players_new_income, no? yep, your right, it should be $players_new_income, I changed that, and the 'money' column is being updated for every user, but it's increasing the 'money' by '10' every time, instead of what the maths really would make the income be. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368292 Share on other sites More sharing options...
MasterACE14 Posted October 13, 2007 Author Share Posted October 13, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368413 Share on other sites More sharing options...
MasterACE14 Posted October 13, 2007 Author Share Posted October 13, 2007 When I turn error reporting on 'E_ALL' I get these errors: Notice: Undefined variable: player_weaponid in /home/ace/public_html/conflictingforces/cron.php on line 62 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_armorid in /home/ace/public_html/conflictingforces/cron.php on line 63 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_vehicleid in /home/ace/public_html/conflictingforces/cron.php on line 64 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_weaponid in /home/ace/public_html/conflictingforces/cron.php on line 67 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_armorid in /home/ace/public_html/conflictingforces/cron.php on line 68 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_vehicleid in /home/ace/public_html/conflictingforces/cron.php on line 69 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_weaponid in /home/ace/public_html/conflictingforces/cron.php on line 72 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_armorid in /home/ace/public_html/conflictingforces/cron.php on line 73 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Notice: Undefined variable: player_vehicleid in /home/ace/public_html/conflictingforces/cron.php on line 74 Notice: Undefined index: in /home/ace/public_html/conflictingforces/functions.php on line 99 Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368436 Share on other sites More sharing options...
MasterACE14 Posted October 13, 2007 Author Share Posted October 13, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368465 Share on other sites More sharing options...
MasterACE14 Posted October 13, 2007 Author Share Posted October 13, 2007 I'm guessing I've made an error with the my custom function. But then what should the code really be?? Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368499 Share on other sites More sharing options...
MasterACE14 Posted October 13, 2007 Author Share Posted October 13, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368817 Share on other sites More sharing options...
MasterACE14 Posted October 14, 2007 Author Share Posted October 14, 2007 Everything is working perfectly now, in a few of my function calls I missed the $all_ at the beginning of them. Thanks Everyone, Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-368873 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Share Posted October 14, 2007 Glad you got it working, I realize this code is for some kind of Game, let me know when it's done - I love playing around with PHP based Games. Quote Link to comment https://forums.phpfreaks.com/topic/72913-solved-mysql-update-undesired-result/#findComment-369113 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.