|
|
|
|
![]() ![]() |
Aug 10 2007, 12:51 PM
Post
#1
|
|
|
Member - Active Contributor Group: Members Posts: 91 Joined: 18-May 07 Member No.: 22,008 |
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. CODE $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. |
|
|
|
Aug 11 2007, 01:30 AM
Post
#2
|
|
|
Member - Active Contributor Group: Members Posts: 91 Joined: 18-May 07 Member No.: 22,008 |
By the way, it isn't really updating everyday, it only updates when a user logs in to the site in a different day than the day found in the mySQL database.
|
|
|
|
Aug 11 2007, 03:58 AM
Post
#3
|
|
|
Advanced Member Group: Members Posts: 170 Joined: 30-July 07 Member No.: 23,704 |
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.
QUOTE $date = date("d"); So in your codes, you don't have to state the time and date, instead put it in cron to do the job. $month = date("m"); $sql = "SELECT * FROM `refresh` WHERE `date` = '$date' AND `month` = '$month'"; $result = mysql_query($sql); QUOTE $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 |
|
|
|
Aug 11 2007, 10:05 AM
Post
#4
|
|
|
Member - Active Contributor Group: Members Posts: 91 Joined: 18-May 07 Member No.: 22,008 |
Ah, I didn't know there was a Cron function in PHP. I'll try to find a place to learn about it. Thanks for the comment. ^^
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 14th October 2008 - 01:35 AM |