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). ??? 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 } 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) ?> 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 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 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! 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
Archived
This topic is now archived and is closed to further replies.