Jump to content

Limit entries in database?


sanleless

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/66992-limit-entries-in-database/
Share on other sites

<?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.

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.

 

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.