Log in

View Full Version : Database Changes At Scheduled Times



Titan85
08-17-2007, 05:54 PM
Well, I am making a script that will update all users ranks every 7 days. What I came up with was to just include this page in another page that will be accessed all the time:
<?php

$get = mysql_query("SELECT id, level, last_pro, signup FROM `users` WHERE status = 'active'") or die ("Error Getting User Data! \n<br />\n" .mysql_error());

while ($u = mysql_fetch_array($get)) {
if ( !$last_pro && $signup >= (time()-60*60*24*7) ) { $level = $u['level'] + 1; $last_pro = date('n/j/y [g:i A]'); }
else if ( $last_pro >= (time()-60*60*24*7) ) { $level = $u['level'] + 1; $last_pro = date('n/j/y [g:i A]'); }
$update = mysql_query("UPDATE `members` SET level = '$level', last_pro = '$last_pro' WHERE id = '$id'") or die ("Error Updating Levels! \n<br />\n" .mysql_error());
}

?>I am not 100% sure that this will work. Will that script add 1 to a user level 7 days after their previous promotion? Also is including this in a commonly accessed page the best way to do this? Thanks in advance

Twey
08-17-2007, 06:25 PM
Try:
UPDATE users SET level = level + 1, last_pro = NOW() WHERE status = active AND DATE_SUB(NOW(), INTERVAL 7 DAY) > last_pro;

Titan85
08-19-2007, 11:11 PM
Ok, that helps me out a lot and cuts down the code significantly :). Can I use this code to also delete users after 8 days of inactivity by changing it to this:
DELETE FROM `users` WHERE status = active AND DATE_SUB(NOW(), INTERVAL 8 DAY) > last_login;I ask just because it will take a while to know for sure if it works (the span of time in the script). Let me know, thanks.

Twey
08-20-2007, 02:58 AM
Yes.&#32;