lonelym 0 Report post Posted August 10, 2007 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
lonelym 0 Report post Posted August 11, 2007 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. Share this post Link to post Share on other sites
kelvinmaki 0 Report post Posted August 11, 2007 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
lonelym 0 Report post Posted August 11, 2007 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. ^^ Share this post Link to post Share on other sites