iRoot121 Posted January 6, 2014 Share Posted January 6, 2014 Hai guys, I'm programming something like a text-based game now, but I've a question.. I want to have a script running, that updates every hour (01:00, 02:00, etc.) the values of some MySQL tables. I've now something like this, but I don't know if this is the right way to do it. So do you guys have any tips to do something like this? Thanks in advance. Updater.php: if (date("i", time()) == "00") { while($x = mysql_fetch_object($dbres)) $update[$x->name] = $x->time; if(floor($update['hour']/3600) != floor(time()/3600)) { $dbres = mysql_query("SELECT GET_LOCK('hour_update',0)"); if(mysql_result($dbres,0) == 1) { $cron_pass = "secretcronpassword"; mysql_query("UPDATE `cron` SET `time`='".time()."' WHERE `name`='hour'"); include("_cron_hour.php"); mysql_query("SELECT RELEASE_LOCK('hour_update')"); } } } _cron_hour.php: if($cron_pass != "secretcronpassword") exit; $dbres = mysql_query("SELECT * FROM `aandelen`"); while($aandeel = mysql_fetch_object($dbres)) { $koersmin = rand(1,500); $koersplus = rand(1,500); mysql_query("UPDATE `aandelen` SET `koers`=`koers`+$koersplus WHERE `naam`='".$aandeel["naam"]."'"); mysql_query("UPDATE `aandelen` SET `koers`=`koers`-$koersmin WHERE `naam`='".$aandeel["naam"]."'"); } if($aandeel["koers"] < 2500) { mysql_query("UPDATE `aandelen` SET `koers`=10000 WHERE `naam`='".$aandeel["naam"]."'"); } $sql1 = mysql_query("SELECT * FROM users WHERE uurloon='1' AND familie <> 'Geen'"); if (mysql_num_rows($sql1)>0) { while($info2 = mysql_fetch_array($sql1)) { $row = mysql_fetch_assoc(mysql_query("SELECT * FROM families WHERE name='".$info2["familie"]."'")); mysql_query("UPDATE users SET cash='".($info2["cash"]+((($row["aandelen"]*200)/100)*25))."', bank='".($info2["bank"]+((($row["aandelen"]*200)/100)*75))."' WHERE login='".$info2["login"]."'"); } } Link to comment https://forums.phpfreaks.com/topic/285141-automatic-query/ Share on other sites More sharing options...
TinyI Posted January 6, 2014 Share Posted January 6, 2014 Hey, a few things I want to point out to you. Your password check is happening a bit too late. By that point, you've locked the table and that exit you have in your code will stop your code dead in its tracks, meaning the release will never happen and so you're a bit stuck! Rare that it will happen, but when it does, you'll be scratching your head for hours. Other thing I suggest is at the very least, change your functions to the MySQLi ones. MySQL functions are deprecated now so save yourself from headache. I'd personally say use something like PDO, as for a text-based game, it could be quite prone to attacks from wannabe hackers trying to learn something (talking from experience). As far as your scripts are concerned though, it seems fine. I would personally use a MySQL transaction as a bit of protection just in case something does mess up somewhere along the way for reason X,Y,Z, you can just do a rollback and throw an exception which notifies you. Better to be paranoid than having bad data. Link to comment https://forums.phpfreaks.com/topic/285141-automatic-query/#findComment-1464102 Share on other sites More sharing options...
iRoot121 Posted January 6, 2014 Author Share Posted January 6, 2014 Thanks for the info, I'll change the MySQL values . Link to comment https://forums.phpfreaks.com/topic/285141-automatic-query/#findComment-1464108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.