Store latest 50 records only

Discussion in 'PHP' started by Om ji Kesharwani, Dec 24, 2010.

  1. #1
    I have a table to store messages. I want to store only latest 50 messages how to delete the records previous to latest 50 records?
     
    Om ji Kesharwani, Dec 24, 2010 IP
  2. JordanPalmer

    JordanPalmer Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just on on the output LIMIT to 50?

    I would need to know your database design to see exactly how to successfully delete them, Usually it's something around
    mysql_query("DELETE FROM Table_name WHERE row_name > 50");
     
    JordanPalmer, Dec 24, 2010 IP
  3. Gediminas

    Gediminas Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Gediminas, Dec 24, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    It depends.

    Start by running the following query on the database, replacing "TABLE_NAME" with your message tables name, and post the results. If you're using something like phpMyAdmin, a screenshot of the table's structure from there will work too.

    DESCRIBE TABLE_NAME;
    Code (sql):
    The result from that SQL query will look something like this

    mysql> describe visitors;
    +---------+-------------+------+-----+---------+-------+
    | Field   | Type        | Null | Key | Default | Extra |
    +---------+-------------+------+-----+---------+-------+
    | keyword | varchar(32) | NO   |     | NULL    |       | 
    | userid  | int(11)     | NO   |     | NULL    |       | 
    +---------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    Code (markup):
     
    joebert, Dec 24, 2010 IP
  5. PixyPalace

    PixyPalace Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yah we need ur table stuff
     
    PixyPalace, Dec 24, 2010 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    What you want to achieve will not work unless you have a date/timestamp field.

    Unless ofcourse the unique keys are generated by you and not automatically, then it depends on the algorithm you are using to create the keys.
     
    ThePHPMaster, Dec 24, 2010 IP
  7. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks to all.I done it...
    $max_id=mysql_fetch_array(mysql_query("SELECT count(id) as id FROM mytable"));
    $number_of_records=$max_id[id];
    $limit=$number_of_records-50;
    if(!($limit<1))
    {
    mysql_query("DELETE FROM mytable ORDER BY id ASC LIMIT $limit")or die(mysql_error());
    }
     
    Om ji Kesharwani, Dec 28, 2010 IP