scarhand Posted September 24, 2007 Share Posted September 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70503-solved-removing-oldest-table-row-when-new-one-is-posted/ Share on other sites More sharing options...
freakstyle Posted September 24, 2007 Share Posted September 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70503-solved-removing-oldest-table-row-when-new-one-is-posted/#findComment-354177 Share on other sites More sharing options...
scarhand Posted September 25, 2007 Author Share Posted September 25, 2007 I was thinking of doing something like that however, I am still new to this stuff and was wondering if there was already a function this could be accomplished with.... Quote Link to comment https://forums.phpfreaks.com/topic/70503-solved-removing-oldest-table-row-when-new-one-is-posted/#findComment-354488 Share on other sites More sharing options...
cooldude832 Posted September 25, 2007 Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70503-solved-removing-oldest-table-row-when-new-one-is-posted/#findComment-354490 Share on other sites More sharing options...
scarhand Posted September 25, 2007 Author Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70503-solved-removing-oldest-table-row-when-new-one-is-posted/#findComment-354556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.