Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts

    Delete Old Records

    Hello All,

    In this Code below , am trying to delete the Old Records , its to delete all records were inserted before 5 days or any X day.. My code works But it deletes 5 records from the total , regrads the date when were added..

    I made it that way Because i could Think how to make it by Date .. the way i save the date like 12736464
    so maybe this
    PHP Code:
    $days time() - 1209600
    PHP Code:
     $con=mysql_connect($host,$dbuser,$dbpass)
     or die(
    "FAILED to connect");
     
    mysql_select_db($dbname,$con)
     or die(
    "can't conect to DB");



     
    $sql =mysql_query("SELECT * FROM test")
      or die(
    "can't select the test");
       
    $total=mysql_num_rows($sql);

       
    $limit "5";     //5 days

                
    $diff$total $limit ;

                   if (
    $diff<=0) { echo"No old records";}else{
       
    $sql2mysql_query("delete from testwhere .... somthing i dont know how to do it .... LIMIT $diff") or die ("cann't delete");


      if (!
    $sql2){
      echo
    " Could Not delete old records";
      }else{
     echo
    " old recrods deleted";
        }
         } 
    Last edited by ajloun; 02-08-2010 at 04:50 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    If that's unix time format, you're working in seconds, so you need to figure 1 day in seconds, (it's 84,400, if I recall correctly offhand), then times that by days and delete the records where the date in the DB is less than the amount of seconds total for five days.

    It would be something along the lines of:

    Code:
    $days = 5;
    $maxage = ((3600*24)*$days);
    
    $sql = 'DELETE FROM [tablename] WHERE date<'.(time()-$maxage);
    Last edited by MattF; 02-08-2010 at 04:59 AM.

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    thx ,
    did not work quiet will , lets change the queastion .

    i thought about better way , Just delete the Old By ID ..

    PHP Code:
    ORDER BY id ASC 

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    That would work perfectly well if you actually adapted it for your code. What you're asking is basic SQL. I would suggest that doing some revision on that should be your first port of call.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •