The php implementation only keeps the latest 1000 records in mysql. Php implementation only keeps the latest 1000 records in mysql. This article mainly introduces how to keep only the latest 1000 records in mysql and related examples and database structure in php, which is comprehensive, with php, only the latest 1000 records in mysql are retained.
This article mainly introduces how to keep only the latest 1000 records in mysql in php and related examples and database structures. it is very comprehensive and can be referenced by other interested parties.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 database ". $ num. "Record, redundant ". ($ num-$ limit ). "The record has been deleted successfully and is still available ". $ limit. "Record! "; } } Else { Echo "insufficient data records". $ limit! No need to delete! "; } ?> |
Test. SQL
-- PhpMyAdmin SQL Dump
-- Version 3.1.5-rc1
Http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation date: August 19, 2010
-- 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'
--
?
1 2 3 4 5 |
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; |
The above is all the content of this article. I hope you will like it.
This article describes how to keep only the latest 1000 records in mysql in php and related examples and database structures...