adamjones Posted April 11, 2010 Share Posted April 11, 2010 Haven't done a bit of PHP in a good while! Basically, I'm going to run a daily cron job, which runs my PHP script, which will check a database called 'bans', which has 2 rows, 'username' and 'exp_date'. If any dates in the 'exp_date' row are less than, or equal to today, it will remove all those entries, and if not, leave them there. I tried to write a script to do this... I failed, miserably. I just don't know how to do it. Either way, her eis my script, it obviously won't work, lol; <?php session_start(); require_once('config.php'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } date_default_timezone_set('GMT'); $today = date("F j, Y, g:i a"); $qry="SELECT exp_date FROM bans'"; $result=mysql_query($qry); if($exp_date <= $today) { qry2="DELETE FROM bans WHERE exp_date <= '$today'"; $result2=mysql_query($qry2) or die(mysql_error()); } } ?> Could anyone please help me? Thank you! Link to comment https://forums.phpfreaks.com/topic/198228-need-some-help-please/ Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 Here's one way, just use this query DELETE FROM bans WHERE DATE_FORMAT(exp_date, '%Y%m%d') <= DATE_FORMAT(CURDATE(), '%Y%m%d') Link to comment https://forums.phpfreaks.com/topic/198228-need-some-help-please/#findComment-1040054 Share on other sites More sharing options...
TeddyKiller Posted April 11, 2010 Share Posted April 11, 2010 You could perhaps work from something like this. $query = mysql_query("SELECT * FROM bans"); if(mysql_num_rows($query) > 0){ while($row = mysql_fetch_array($query)) { if($row['exp_date'] < date("F j, Y, g:i a", time())){ $query1 = mysql_query("DELETE FROM bans WHERE user_id = '".$row['user_id']."'"); } } } There is probably a better solution, but this is the way I do it. (replacing user_id with the collum name for the users id, or username in your db table. Link to comment https://forums.phpfreaks.com/topic/198228-need-some-help-please/#findComment-1040055 Share on other sites More sharing options...
adamjones Posted April 11, 2010 Author Share Posted April 11, 2010 Thank you both for your suggestions, I shall try them out Link to comment https://forums.phpfreaks.com/topic/198228-need-some-help-please/#findComment-1040070 Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 You could perhaps work from something like this. $query = mysql_query("SELECT * FROM bans"); if(mysql_num_rows($query) > 0){ while($row = mysql_fetch_array($query)) { if($row['exp_date'] < date("F j, Y, g:i a", time())){ $query1 = mysql_query("DELETE FROM bans WHERE user_id = '".$row['user_id']."'"); } } } There is probably a better solution, but this is the way I do it. (replacing user_id with the collum name for the users id, or username in your db table. This is bad advice. You're selecting all records, then using PHP to loop through them to compare the date. That's what the MySQL WHERE clause is for. Link to comment https://forums.phpfreaks.com/topic/198228-need-some-help-please/#findComment-1040073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.