PHP implementation only keeps the latest 1000 records in mysql_PHP tutorial

WBOY
Release: 2016-07-13 09:50:19
Original
903 people have browsed it

php implementation only retains the latest 1000 records in mysql

This article mainly introduces the method and related examples and database structure of php implementation to retain only the latest 1000 records in mysql. It is very comprehensive, friends in need can refer to it.

 ?

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");

//保留最新的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."条!没有必要删除!";

}

?>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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;

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 "There are original ".$num." records in the database, and the extra ".($num-$limit)." records have been successfully deleted, and now there are still ".$limit." records left!"; <🎜> <🎜>}<🎜> <🎜>}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 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`  --  ?
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 the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1018368.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1018368.htmlTechArticlephp implementation only retains the latest 1000 records in mysql. This article mainly introduces the php implementation to retain only the latest 1000 records in mysql. The record method and related examples and database structure are very comprehensive, including...
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