PHP implementation only retains the latest 1000 records in mysql, mysql1000_PHP tutorial

WBOY
Release: 2016-07-13 09:49:43
Original
906 people have browsed it

php implementation only retains the latest 1000 records in mysql, mysql1000

<&#63;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."条!没有必要删除!";
}
&#63;>

Copy after login

test.sql

-- phpMyAdmin SQL Dump
-- version 3.1.5-rc1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generated date: August 19, 2010 05:47
-- Server version: 5.0.18
-- PHP version: 5.2.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `test`
--

-------------------------------------------------- ----------

--
-- Table structure `news`
--

CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
Copy after login

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1019070.htmlTechArticlephp implementation only retains the latest 1000 records in mysql, mysql1000 php mysql_connect("localhost","root", "root");mysql_select_db("test");//Keep the latest 1000 records $limit=1000;$query="...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template