Jump to content

[SOLVED] removing oldest table row when new one is posted


scarhand

Recommended Posts

this is probably a really simple answer but im trying to figure out how to make it so that a user can pick how many rows they want to keep stored in a database table before an old one is automatically deleted

 

im guessing the query would be made, then a while loop would determine how many......ahhh i dont know there has to be a simpler way of doing it....any help would be appreciated.

Link to comment
Share on other sites

Hey there,

 

sounds like you would just need to determine how many rows the user wants to allow, how many are found and then delete the ones over the allotted amount.

 

<?php
$UserAllowedMaxRows = 5 ; 

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);  // or whatever you need to pull 
$num_rows = mysql_num_rows($result);

if ( !is_null( $num_rows ) && $num_rows >  $UserAllowedMaxRows ) :
// enter your logic to remove the offending rows you don't want
// might be helpful to create an object or an array of your records from the initial query so you can use that data 
// to determine what to delete vs sending another select here
endif ; 

ref: http://us3.php.net/manual/en/function.mysql-num-rows.php

 

Good luck

Link to comment
Share on other sites

my question is what is the point of this, data in a table doesn't hurt, and deleting a few rows at a time does nothing.  If you want to really make a table purger make a cron job that runs once a month that deletes all records that are X days old or keep only Y rows where Y > 50,000 because otherwise deleting a few rows means nothing, the db for these forums probably tops a billion records easily.

Link to comment
Share on other sites

my question is what is the point of this, data in a table doesn't hurt, and deleting a few rows at a time does nothing.  If you want to really make a table purger make a cron job that runs once a month that deletes all records that are X days old or keep only Y rows where Y > 50,000 because otherwise deleting a few rows means nothing, the db for these forums probably tops a billion records easily.

 

thank you very much for the sound advice. i will just forget about scripting this feature.

Link to comment
Share on other sites

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.