Jump to content
xisto Community
Sign in to follow this  
lonelym

Little Manipulating Of Time Newbish

Recommended Posts

So, I've been wondering for months how I would be able to let users' data in mySQL to update by themselves (Like letting HP return to its max value every day by itself, without me doing anything), and I have finally found a little trick on how to do it. Yes, its only a newbie-ish fourteen year old's attempt to make his coding easier.

 

First, you need to create a table, let's call it table refresh. Table refresh is where we put all the times we refreshed the users' certain data. Next, you need to write this down in your config.php or dbCon.php or any page that is always INCLUDEd in every page.

 

Remember, your web page must be connected to the database.

$date = date("d");$month = date("m");$sql = "SELECT * FROM `refresh` WHERE `date` = '$date' AND `month` = '$month'";$result = mysql_query($sql);if (mysql_num_rows($result) == 0){$sql1 = "INSERT INTO `refresh` VALUES ('$date', '$month', '')";$result1 = mysql_query($sql1);$sql2 = 'UPDATE users SET `field` = `value`;';$result2 = mysql_query($sql2);if ($result2 && $result1){die( text );}}
Okay, now to dissect each and line.

 

The first line is just setting the variable $date to the date of when the dbCon/config.php was executed.

The second line is the same, but its for the month type.

The next line is a written SQL query (I don't like writting everything in one variable).

The next is writing executing the query.

Now we check if the executed query had any rows/data.

NOTE!

if there are any data, it will skip the if statement.

 

Since there are no rows/data found for today. Then, it creates the new row/data for today.

After that, it executes the query.

Then you can write what you want to update in the table.

You can add the die() function as well if you want to notify the person who executed the page about the edited variables.

 

If anyone can give a better explanation (I know it was really confusing, I myself had troubles with it), feel free to post.

Share this post


Link to post
Share on other sites

Another way to manipulating of data in the background without having the user to login to refresh the database table is by using crontab or cron. Basically Cron is a scheduler that set the time and date when you want to execute certain query.

$date = date("d");$month = date("m");
$sql = "SELECT * FROM `refresh` WHERE `date` = '$date' AND `month` = '$month'";
$result = mysql_query($sql);

So in your codes, you don't have to state the time and date, instead put it in cron to do the job.

$sql2 = 'UPDATE users SET `field` = `value`;';

Then define which query to execute when the date/time met. eg. your update queries.
In the case, your codes will be more readable and effective. Any thoughts from others? Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.