Jump to content

Recommended Posts

To start with, the date() format you are producing cannot be used in a greater-than/less-than comparison, because the h (12 hour) and a (am/pm) format does not produce a string that can be compared with another string and give the correct results. Comparing 2011-08-17 02:45:10 pm with 2011-08-17 03:45:10 am (differences between the two values are in red) will indicate that the second (earlier value) is greater than the first (later value) because 03 (the hours) is greater-than 02.

 

You would need to use a format like 'Y-m-d H:i:s' (which is a mysql DATETIME value) and the `time` column in your table would need to be the same format as well (hopefully it is already a mysql DATETIME field.)

The second problem with what you are doing is that date/time values in the past are smaller/less-than later date/time values.

 

To find rows where the `time` field (assuming this is a mysql DATETIME value) is older than (already gone past) two days ago, you would first need to form a date/time that is two days ago (i.e. 2011-08-13 14:45:10), and then test if the `time` field is less-than that value. The WHERE clause would look like -

 

WHERE `time` < '2011-08-13 14:45:10'

 

The above WHERE clause will match rows with `time` older than two days ago. Rows with `time` greater-than or equal to two days ago will not be matched and will remain in the table after you perform the DELETE query.

so i did this then

 

		$time_count = 60; 
		$match_time = time() + $time_count; 
		$sql="SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` > '".$match_time."'";
		echo $sql;
		$result = mysql_query($sql) or die(mysql_error());
		if(!$result){
		//echo 'SELECT failed: '.mysql_error();
		}else{
		while($row = mysql_fetch_array($result)){
		if (mysql_num_rows($result)) {
		$id = $row['id'];
		if(!unlink($row['image'])){
		//echo "unlink ".$row['image']." failed";
		}else{
		}
		}
		$sql="DELETE FROM tbl WHERE id = '".$id."'";
		$result= mysql_query($sql);
		if(!$result){
		//echo 'DELETE from tbl with id '.$id.' failed: '.mysql_error();
		}
		executeSql($sql);
} 
} 

 

this imminently is deleting anything i post

 

and if i do this

 

		$time_count = 60; 
		$match_time = time() - $time_count; 
		$sql="SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` < '".$match_time."'";
		echo $sql;
		$result = mysql_query($sql) or die(mysql_error());
		if(!$result){
		//echo 'SELECT failed: '.mysql_error();
		}else{
		while($row = mysql_fetch_array($result)){
		if (mysql_num_rows($result)) {
		$id = $row['id'];
		if(!unlink($row['image'])){
		//echo "unlink ".$row['image']." failed";
		}else{
		}
		}
		$sql="DELETE FROM tbl WHERE id = '".$id."'";
		$result= mysql_query($sql);
		if(!$result){
		//echo 'DELETE from tbl with id '.$id.' failed: '.mysql_error();
		}
		executeSql($sql);
} 
} 

 

nothing is ever getting deleted

Since your `time` column is a mysql TIMESTAMP with a format of YYYY-MM-DD HH:MM:SS, why are you trying to compare that with a Unix Timestamp form php's time() function? Those are not even in the same formats and would never compare correctly.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.