php 时间过去后如何删除mysql行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9865393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete mysql row after time passes?
提问by Sweepster
I have no idea where to start with this one:
我不知道从哪里开始这个:
I have a database that stores postID
and Date
.
我有一个存储postID
和Date
.
What I want to do is have my website auto delete all rows where Date
is less than today. This script can't have any user input at all. No button clicks, nothing. The script must run every day at midnight.
我想要做的是让我的网站自动删除Date
比今天少的所有行。这个脚本根本不能有任何用户输入。没有按钮点击,什么都没有。该脚本必须每天在午夜运行。
I've been looking all over the place for something that does this and I've found absolutely nothing.
我一直在到处寻找可以做到这一点的东西,但我一无所获。
回答by Mar Cejas
You can use PHP script and use cron job on your cpanel.
您可以使用 PHP 脚本并在您的 cpanel 上使用 cron 作业。
Example:
例子:
cronjobcommand.php
定时任务命令.php
<?php
include 'your_db_connection';
mysql_query("DELETE FROM your_table_name WHERE Date < NOW()");
?>
I have attached a screenshot below for your more reference.
我在下面附上了截图供您参考。
回答by Vic Seedoubleyew
For those out there who are on a shared hosting, like 1and1's, and can't use cron, here are 2 alternatives :
对于那些在共享主机上的人,比如 1and1's,并且不能使用 cron,这里有 2 个替代方案:
mysql eventsenable you to place a time trigger on mysql, which will execute when you'll want, without having to be fired by any kind of user input
if you cannot create mysql events because you're on 1and1 :(, an alternative is to use webcron
mysql 事件使您能够在 mysql 上放置一个时间触发器,该触发器将在您需要时执行,而无需被任何类型的用户输入触发
如果您因为在 1and1 上而无法创建 mysql 事件 :(,另一种方法是使用webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want
你只需要告诉 webcron 你想要运行的 php 脚本的 url,他们会在你想要的时间间隔为你触发它
回答by Mojtaba Rezaeian
Why using cronjobs everyday?? Why not filter data on output. For example in your select check if post date equals today with adding a simple where
:
为什么每天都使用 cronjobs?为什么不过滤输出数据。例如,在您选择检查发布日期是否等于今天添加一个简单的where
:
SELECT * FROM `posts`
WHERE (DATE(`post_date`) = DATE(NOW()));
This way you're not requiredto do your database managements/cronjobs on any special time and it will be used just for database managements. Afterwards you can delete unnecessary data at any timeusing by mysql command like:
这样您就不需要在任何特殊时间进行数据库管理/cronjobs,它将仅用于数据库管理。之后,您可以随时使用 mysql 命令删除不必要的数据,例如:
DELETE FROM `posts` WHERE (
DATE(`post_date`) < DATE(NOW())
)
回答by sarnold
Most hosts provide a cron(8)
service that can execute commands at specific times. You use the crontab(1)
program to manage the crontab(5)
file the describes when to run which commands.
大多数主机提供cron(8)
可以在特定时间执行命令的服务。您使用该crontab(1)
程序来管理crontab(5)
描述何时运行哪些命令的文件。
There's a lot of functionality available to you, but if you write a program (shell script, php script, C program, whatever) that runs the appropriate MySQL commands, you can call the program via cron(8)
in an entirely hands-off fashion.
有很多功能可供您使用,但是如果您编写了一个运行适当 MySQL 命令的程序(shell 脚本、php 脚本、C 程序等等),您就可以cron(8)
完全不干涉地调用该程序。
Run crontab -e
to edit your current crontab(5)
file. If none exists, hopefully you'll get one with a helpful header. If not, copy this:
运行crontab -e
以编辑当前crontab(5)
文件。如果不存在,希望你会得到一个有用的标题。如果没有,请复制以下内容:
# m h dom mon dow command
The columns indicate the minute, hour, day of month, month, and day of week to execute commands. All the numbers in the columns are essentially ANDed together to decide when to run commands.
列指示执行命令的分钟、小时、月中的某天、月份和星期几。列中的所有数字本质上都是通过 AND 运算来决定何时运行命令的。
Thus, midnight every night would look like this:
因此,每天晚上的午夜看起来像这样:
0 0 * * * /path/to/executable
It's remarkably flexible, so put some time into the documentation, and you'll find many uses for it.
它非常灵活,所以花一些时间在文档中,你会发现它有很多用途。
回答by sarnold
You should set cron
job (scheduled tack.) for it.
你应该cron
为它设置工作(预定的粘性。)。
A cron jobis an automated program setup for Linux and Unix operating systems. It allows the user to execute several commands or functions at a specific time and date.
一个cron作业是一个自动化的程序设置为Linux和Unix操作系统。它允许用户在特定的时间和日期执行多个命令或功能。
you have cron Job in your cpanel setup. first you need to make a php script with your logic for delete record after each date. take date from server and write script for delete.
您的 cpanel 设置中有 cron Job。首先,您需要使用您的逻辑制作一个 php 脚本,以便在每个日期之后删除记录。从服务器获取日期并编写删除脚本。
then go to cron tab in your cpanel and do settings for time interval to run cron and give path of your php script file.
然后转到cpanel中的cron选项卡并设置时间间隔以运行cron并提供php脚本文件的路径。
回答by Skrol29
MySQL doesn't have a task scheduler. So you have to use the task scheduler of your Operating System (CRONunder Linux), or to lunch a basic task checker sub-script during the script of the main page (on another page that is supposed to display the changing data).
MySQL 没有任务调度程序。因此,您必须使用操作系统的任务调度程序(Linux 下的CRON),或者在主页面的脚本期间(在另一个应该显示更改数据的页面上)使用基本的任务检查器子脚本。