danp Posted August 17, 2009 Share Posted August 17, 2009 Alrighty, I'm trying to make a script for a game I'm helping with... the script should be able decrease the number in a rain (rains are when players have a chance to find special items) by 1 in the database every hour in a cron job, and display the remaining number of hours left in the chat (also in the database).. RainDay *MUST* equal 1 for the rain to take place, and RainHours needs to be between 1 to 24 for it to be correct...my script kind of works, except it usually sets the RainDay to 0 before the hours are down to 0, and it doesn't correctly display the number of hours left in chat, just shows a blank space as in "There are hours left in this rain!" etc... Any tips or tricks I should be aware of? I appreciate any and all suggestions. Below is the code I currently use. <?php include ("connect.php"); $Query = "SELECT * FROM `SpecialEvent`"; $Query2=mysql_query($Query) or die("Can't find what you're looking for. Try again."); $Special=mysql_fetch_array($Query2); if ($Special[RainDay]==0) {exit;} if ($Special[RainDayHours]==0) {exit;} if ($Special[RainDayHours] >= 1){ mysql_query("UPDATE SpecialEvent SET RainDayHours=RainDayHours-'1'")or die(mysql_error()); $Hours = mysql_query("SELECT 'RainDayHours' FROM 'SpecialEvent'"); mysql_query("insert into chat (chat) value('<font style=\"color: #00FFFF\">There are $Hours hours left in this rain!</font>')");}; $Hours = mysql_query("SELECT 'RainDayHours' FROM 'SpecialEvent'"); if ($Hours = 0){mysql_query("UPDATE SpecialEvent SET RainDay = '0'"); mysql_query("insert into chat (chat) value('<font style=\"color: #00FFFF\">The rain has ended..</font>')"); }exit; ?> I'm a nub, I know Link to comment https://forums.phpfreaks.com/topic/170584-logic-problem/ Share on other sites More sharing options...
DavidAM Posted August 17, 2009 Share Posted August 17, 2009 $Hours = mysql_query("SELECT 'RainDayHours' FROM 'SpecialEvent'"); $Hours is the query Resource, NOT the RainDayHours value. You need to call mysql_fetch_array to get the values returned by the query. Also, why are you selecting Hours again two lines later? I doubt that it has changed in between. I think I would change that last part to: if ($Special[RainDayHours] >= 1){ mysql_query("UPDATE SpecialEvent SET RainDayHours=RainDayHours - 1")or die(mysql_error()); $Special[RainDayHours] -= 1; // changed in Database so change it here, too. mysql_query("insert into chat (chat) value('<font style=\"color: #00FFFF\">There are $Special[RainDayHours] hours left in this rain!</font>')"); }; if ($Special[RainDayHours] == 0){ mysql_query("UPDATE SpecialEvent SET RainDay = '0'"); mysql_query("insert into chat (chat) value('<font style=\"color: #00FFFF\">The rain has ended..</font>')"); } Oh, I didn't see the closing braces before I re-worked it. Yeah, I don't think you need to re-query the database for the hours so many times; unless it might be changing from some other process (which seems unlikely). Link to comment https://forums.phpfreaks.com/topic/170584-logic-problem/#findComment-899771 Share on other sites More sharing options...
danp Posted August 17, 2009 Author Share Posted August 17, 2009 Hey David. Thanks so much for helping me out! The script works smoothly now. Sincerely, Dan Link to comment https://forums.phpfreaks.com/topic/170584-logic-problem/#findComment-899819 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.