Php implementation only keeps the latest 1000 records in mysql, mysql1000 _ PHP Tutorial

Source: Internet
Author: User
In php, only the latest 1000 records and mysql records are retained. Php only keeps the latest 1000 records in mysql, mysql1000 phpmysql_connect (localhost, root, root), mysql_select_db (test), and the latest 1000 records $ limit1000; $ query php: only the latest 1000 records and mysql records are retained.
<? 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" database has been ". $ 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'
--

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.

Keep php mysql_connect ("localhost", "root", "root"); mysql_select_db ("test"); // keep the latest 1000 records $ limit = 1000; $ query = "...

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.