Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Little Manipulating Of Time, Newbish
lonelym
post Aug 10 2007, 12:51 PM
Post #1


Member - Active Contributor
Group Icon

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.
Go to the top of the page
 
+Quote Post
lonelym
post Aug 11 2007, 01:30 AM
Post #2


Member - Active Contributor
Group Icon

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.
Go to the top of the page
 
+Quote Post
kelvinmaki
post Aug 11 2007, 03:58 AM
Post #3


Advanced Member
Group Icon

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");
$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.

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
Go to the top of the page
 
+Quote Post
lonelym
post Aug 11 2007, 10:05 AM
Post #4


Member - Active Contributor
Group Icon

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. ^^
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How To Make Both Windows Look Like Active At Same Time(7)
  2. Back After Long Time(1)
  3. Best Games Of All Time(15)
  4. Timeshift Discussion(2)
  5. Task Mgr. Has Been Disabled By Your Administrator(6)
  6. Hinduism - The Breeder Of World Religions?(23)
  7. Time Travel(10)
  8. Time Travel?(1)
  9. Question About Asp Now() Function(6)
  10. 101 Css Techniques Of All Time(8)
  11. Living Without Computer Today - Is It Possible?(41)
  12. Asp Date And Time Tutorial(0)
  13. Listening To Music Over A Home Network(16)
  14. Counting Down To A Time(7)
  15. Best Games Of All Time(5)
  1. Tremulous Is A Free And Open Source Team-based First-person Shooter With Real-time Strategy Elements(0)
  2. Change Server Time?(9)
  3. Xbox Live. Madden 2005 Money Plays(1)
  4. Time Is Money(4)
  5. Video Card Battle: ATI vs. N-Vidia(34)
  6. Time Travel(23)
  7. Time Shift(2)
  8. Very Addictive Ball Game(15)
  9. Windows Time Problem!(14)
  10. 5 Tips To Evade Rapidshare Waiting Time.(9)
  11. Standard Time Questions(0)
  12. Zplanner -- Free Time Management Software(0)
  13. What Is A Real-time Operating System?(4)
  14. Typer Shark(0)
  15. Browsers Take Much Time To Open(1)


 



- Lo-Fi Version Time is now: 14th October 2008 - 01:35 AM