tqla Posted January 15, 2009 Share Posted January 15, 2009 Hello. Is possible to limit the number of rows in a table to 100? Like, before a new row is added, count the existing rows, if there are already 100, delete row 100 (the oldest row), and add the new row to the top (newest). ??? Quote Link to comment https://forums.phpfreaks.com/topic/140888-solved-limit-mysql-table-to-100-entries/ Share on other sites More sharing options...
ngreenwood6 Posted January 15, 2009 Share Posted January 15, 2009 You can look into the mysql_num_rows function for that. A basic example would be: $query = "SELECT * FROM table"; $results = mysql_query($query); $num_rows = mysql_num_rows($results); if($num_rows == 100) { //put delete query here //put insert query here } Quote Link to comment https://forums.phpfreaks.com/topic/140888-solved-limit-mysql-table-to-100-entries/#findComment-737415 Share on other sites More sharing options...
Philip Posted January 15, 2009 Share Posted January 15, 2009 Or, if you wanted to keep all your data, like me and become an e-packrat. A query would become (no DELETE query required): SELECT * FROM `table` ORDER BY `id` DESC LIMIT 100 You wouldn't lose any data - but if your point is to save space, ngreenwood6 is pretty much correct... I would just change it to: <?php $query = "SELECT * FROM table"; $results = mysql_query($query); $num_rows = mysql_num_rows($results); if($num_rows == 100) { //put delete query here (only deletes if 100 rows were found) } //put insert query here (always inserts) ?> Quote Link to comment https://forums.phpfreaks.com/topic/140888-solved-limit-mysql-table-to-100-entries/#findComment-737417 Share on other sites More sharing options...
tqla Posted January 15, 2009 Author Share Posted January 15, 2009 Thanks ngreenwood6! That did it. e-packrat! lol. Thanks KingPhilip Quote Link to comment https://forums.phpfreaks.com/topic/140888-solved-limit-mysql-table-to-100-entries/#findComment-737421 Share on other sites More sharing options...
ngreenwood6 Posted January 15, 2009 Share Posted January 15, 2009 king phillip is right put the insert query outside of the "if" statement. Sorry about that its getting late lol Quote Link to comment https://forums.phpfreaks.com/topic/140888-solved-limit-mysql-table-to-100-entries/#findComment-737423 Share on other sites More sharing options...
tqla Posted January 15, 2009 Author Share Posted January 15, 2009 Ah ha. Got it. Thank you both! Quote Link to comment https://forums.phpfreaks.com/topic/140888-solved-limit-mysql-table-to-100-entries/#findComment-737426 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.