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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.