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.

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.