PFMaBiSmAd Posted August 15, 2011 Share Posted August 15, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257737 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2011 Share Posted August 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257743 Share on other sites More sharing options...
$php_mysql$ Posted August 15, 2011 Author Share Posted August 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257749 Share on other sites More sharing options...
AyKay47 Posted August 15, 2011 Share Posted August 15, 2011 YOU TOLD IT TO DELETE ANYTHING WITHIN 60 SECONDS... Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257750 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2011 Share Posted August 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257754 Share on other sites More sharing options...
$php_mysql$ Posted August 15, 2011 Author Share Posted August 15, 2011 right said bro i realized it now so i created another row name `expire` int(100) NOT NULL, and inserted it using time() function and now it is doing what i need it to :-D Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257755 Share on other sites More sharing options...
$php_mysql$ Posted August 15, 2011 Author Share Posted August 15, 2011 thanks you both soo much for all the time and help you gave me, finally i got it sorted cheers \m/ Quote Link to comment https://forums.phpfreaks.com/topic/244833-is-this-logic-correct/page/2/#findComment-1257757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.