1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to limit number of rows in MySQL

Discussion in 'PHP' started by bobby9101, Mar 14, 2007.

  1. #1
    Hello, I would like to add a little feature to my site that I can just click a button and then add a row to mysql. But I want a maximum of 20 rows, and when the 21st is submitted the oldest one gets removed.
    How do I do this? Thanks
     
    bobby9101, Mar 14, 2007 IP
  2. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Well you don't have to delete them if you don't want. When calling on the table just limit it to the latest 20 entries. If you want to have them deleted then use mysql_num_rows to get the number of rows and delete accordingly.
     
    smatts9, Mar 14, 2007 IP
    bogart likes this.
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, Just wondering if there was a faster way than counting then delteing by date.
    And yes, i want to delete, not just limit
     
    bobby9101, Mar 14, 2007 IP
  4. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #4
    Simply add "Limit 0,20" to the SQL statement.
    I think that should work.
     
    papa_face, Mar 14, 2007 IP
    bogart likes this.
  5. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks, but as I said earlier I want to delete not limit
     
    bobby9101, Mar 14, 2007 IP
  6. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #6
    set the id autoincrement in DB. Then try this:

    $s = "insert into table_1........."; // to insert new row


    $r = mysql_fetch_array(mysql_query("select min(id) from table_1")); // to find out the oldest row
    $s = $r[0];

    $d = "delete from table_1 where id = $s"; // to delete the row

    Hope it will work
     
    srobona, Mar 14, 2007 IP