how to make mysql update automaticly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

how to make mysql update automaticly

Post by snpo123 »

Hey, I am trying to get mysql to reset a certian number in the database after every 24 hours. What I am trying to do is create a hit counter that not only displays the total number of hits but also the hits recieved today. I want this number to reset every 24 hours, but I dont know how. Can anybody here help me? Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

just store the last time the number was added to, and reset it if the last time is "greater" than "now"
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

Post by snpo123 »

yeah but wouldnt that require a script of some sort that is always running? I mean I cant just manualy update the database everyday, so how do I get it done automaticly?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that does it automatically when we are adding the next hit into the database..
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

Post by snpo123 »

but I have also seen that on some sites they have a timer counting down until something in the database is changed. When the timer hits 0, the data is changed. How do they get it to automaticlly do that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

example:

Code: Select all

$query = mysql_query("SELECT last_hit_time,day_hit_count,hit_count FROM hit_table WHERE 1 LIMIT 1") or die(mysql_error());
list($last_time,$day_count,$count) = mysql_fetch_row($query) or die(mysql_error());
$last_time = explode(":",$last_time);
$last_time = mktime($last_time[0],$last_time[1],$last_time[2],1,1,2000);
$now = mktime(date("H"),date("i"),date("s"),1,1,2000);
if($now < $last_time)
$day_count = 1;
else
$day_count++;
$count++;
$query = mysql_query("UPDATE hit_table SET last_hit_time = NOW(), day_hit_count = '$day_count', hit_count = '$count' LIMIT 1") or die(mysql_error());
if(mysql_affected_rows($query) > 0)
echo "worked";
else
echo "didn't work";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

depending on implementation... they have a date field in their database, that controls when something becomes "visible"..

The count-down stuff is usually done through either javascript or flash.
Post Reply