Jump to content

Need some help please! :(


adamjones

Recommended Posts

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.