sanleless Posted August 28, 2007 Share Posted August 28, 2007 Im using a guest book script that uses a Mysql database. Id like to limit the number of entries it will save in that database to say, 100. I emailed the fella who wrote the script and he replied with: Just make a simple if the id is bigger than 100 or how many entries you want ... then just redirect to the index page. I dont know a whole lot about PHP although Im learning and this is what Im trying which isnt working. if ($id > 100){ echo "<meta http-equiv=refresh content=1; url=http://www.mysite.com/blah>"; } Any help on the proper way of doing this would be much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/66992-limit-entries-in-database/ Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2007 Share Posted August 28, 2007 <?php //first you need to get how many entries you have in the database $query = mysql_query("SELECT COUNT(*) FROM guestbook")or die(mysql_error()); $num = mysql_num_rows($query); if ($num > 100){ echo "<meta http-equiv=refresh content=1; url=http://www.mysite.com/blah>"; } ?> Instead of redirecting them, I would personally put a message...otherwise they won't know what the heck happened, lmao. Quote Link to comment https://forums.phpfreaks.com/topic/66992-limit-entries-in-database/#findComment-335932 Share on other sites More sharing options...
sanleless Posted August 28, 2007 Author Share Posted August 28, 2007 Well that helped some THANKS! Least now it doesnt throw me errors, heh, but now it doesnt post any new messages. What I mean is I want it to drop off old messages after 100 and continue posting new ones Quote Link to comment https://forums.phpfreaks.com/topic/66992-limit-entries-in-database/#findComment-335949 Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2007 Share Posted August 28, 2007 Oh okay, I see what you mean. <?php //first you need to get how many entries you have in the database $query = mysql_query("SELECT COUNT(*) as num, MIN(id) as start FROM guestbook")or die(mysql_error()); $row = mysql_fetch_assoc($query); //Store the ID of the first record number in a var. $start = $row['start']; //How many records you want to delete each time $del_count = 50; //Get the ID of the record you want to start deleting from $del_num = $start + $del_count; if ($row['num'] > 100){ //delete some old entries mysql_query("DELETE FROM guestbook WHERE id < $del_num")or die(mysql_error()); } ?> That will delete the first 50 comments, but I'm assuming you have an auto incremented field that stores the comments unique ID. I called this row "id" in the queries. Quote Link to comment https://forums.phpfreaks.com/topic/66992-limit-entries-in-database/#findComment-335955 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.