<div class="jb51code">
<pre class="brush:PHP;">
<?php
mysql_connect("localhost","root","root");
mysql_select_db("test");
//保留最新的1000条记录
$limit=1000;
$query="select id
from news
";
$result=mysql_query($query);
$num=mysql_num_rows($result);
if($num>$limit){
$query="select id
from news
order by id
desc limit ".$limit;
$result=MysqL_query($query);
MysqL_data_seek($result,$limit-1);
$data=MysqL_fetch_array($result);
$query="delete from news
where id
<'$data[id]'";
if(mysql_query($query)){
echo "数据库中原有".$num."条记录,多余的".($num-$limit)."条记录被成功删除,现在还剩余".$limit."条记录!";
}
}else{
echo "数据记录不足".$limit."条!没有必要删除!";
}
?>