PHP implementation only retains the latest 1000 records in MySQL

Source: Internet
Author: User
Tags phpmyadmin

This article mainly introduces the PHP implementation only to retain the latest 1000 records in MySQL methods and related examples and database structure, very comprehensive, the need for small partners can refer to.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 <?php mysql_connect ("localhost", "root", "root"); mysql_select_db ("test"); Keep the latest 1000 records $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 "original in Database". $num. " Record, superfluous. " ($num-$limit). " The record was successfully deleted and there is still ". $limit." Record! "; } } else{echo "Insufficient data logging". $limit. " Article No need to delete! "; } ? >

Test.sql

--phpMyAdmin SQL Dump

--Version 3.1.5-rc1

--Http://www.phpmyadmin.net

--

--Host: localhost

--Date Created: 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 '

--

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

--

--The structure of the table ' News '

--

?

1 2 3 4 5 CREATE TABLE IF not EXISTS ' news ' (' id ' int (one) unsigned not null auto_increment, ' title ' varchar (256) NOT NULL, PRIMARY KEY (' id ') engine=innodb DEFAULT charset=gb2312;

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.