Page 1 of 1
how to make mysql update automaticly
Posted: Wed May 19, 2004 12:21 am
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.
Posted: Wed May 19, 2004 12:27 am
by feyd
just store the last time the number was added to, and reset it if the last time is "greater" than "now"
Posted: Wed May 19, 2004 12:34 am
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?
Posted: Wed May 19, 2004 12:35 am
by feyd
that does it automatically when we are adding the next hit into the database..
Posted: Wed May 19, 2004 12:37 am
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?
Posted: Wed May 19, 2004 12:54 am
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";
Posted: Wed May 19, 2004 12:56 am
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.