Jump to content

Automatic query.


iRoot121
 Share

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.