Jump to content

Recommended Posts

so i would be deleting entries from my db which are greater than 2 days so i made a function which i call and inside it i added this query, i just want to find out if its a correct logic or will it not work like how i want it to be?

 

$time_count = 2*24*60*60;

$match_time = date('Y-m-d h:i:s', time() + $time_count);

$sql="SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` > '".$match_time."' ORDER BY RAND() LIMIT 1";

Link to comment
https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/
Share on other sites

yes i tested and it does delete entries if i enter a date greater than 2 days in mysql, but im having a feeling that suppose

 

1. user post an entry today and one posts tomorrow

 

by any means will the time_count will auto increase everyday and no post will be deleted?

you are using the time() function here, which the date function uses by default.. the time function uses the unix timestamp of the corrent time, so the timestamp you have specified will always equal the timestamp for the exact time now, plus 2 days..

so i set it to 2 then 19th, 18th and 17th got deleted. but the thing is my time_count will + 2 days everyday but post entry date will always remain same isn't it?

 

when i echo $sql i get this

 

SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` > '2011-08-17 12:01:58' 

 

so tomorrow wont the time_count 2011-08-17 become 2011-08-18 and i make a post today which is 2011-08-15 and will always be the same in db i guess so how will that get deleted? i guess somewhere the logic is not good here?

yes i see what you are saying.. what you will need to do here is compare the date 2 days in the future to the date in your db for date posted..

 

$sql = "SELECT id,image,`time` FROM tbl WHERE `time` > DATE_ADD(NOW(),INTERVAL 2 DAYS)";

 

on second thought.. now you have me confused as to what the ssue is with the way you are doing it now..

so now my query looks like this

 

$time_count = 30; 
		$sql="SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` > 'DATE_ADD(NOW(),INTERVAL ".$time_count." DAY)'";

 

instead of

 

$time_count = 2*24*60*60;
$match_time = date('Y-m-d h:i:s', time() + $time_count);
$sql="SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` > '".$match_time."'";

 

so i removed the $match_time = date('Y-m-d h:i:s', time() + $time_count);

 

am i on the right path?

oh just testing purpose bro, Ok im having an issue testing it now. i made 5 entries on db now how must i check if it is working for changing date is not showing any result of deleting, sorry if im being a pain :-D not getting the idea how to make the interval on the entries

oh yes thats what i want to use but its not doing things how it should nor am i getting the idea how to get it to selects rows in interval from the posted date  to $time_count

 

my time is stored like this

 

`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

 

output is  2011-08-15 18:37:01

ok let me explain again :-) what im trying to achieve is say i make a post today or any day but then it should get deleted after 2 days from the original posted date with a interval of 2 days. so i got this code but im not getting the idea how to select rows with the interval of 2 days and then delete it

 

		$time_count = 2*24*60*60; 
		$match_time = date('Y-m-d h:i:s a', 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);
} 
} 

alright lets work on the logic..

 

$time_count = 2*24*60*60; 			
$match_time = date('Y-m-d h:i:s a', time() + $time_count); 			
$sql="SELECT `id`, `image`,`time` FROM `tbl` WHERE `time` > '$match_time'";			
print $sql;			
$result = mysql_query($sql) or die(mysql_error());			
if(mysql_num_rows($result > 0)){					
while($row = mysql_fetch_array($result)){						
$id = $row['id'];						
$sql1="DELETE FROM tbl WHERE id = '$id'";			
$result1= mysql_query($sql1) or die(mysql_error());						
} 

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.