30分钟后使用Cron作业/事件计划程序删除MySQL行 [英] Delete MySQL Row after 30 minutes using Cron Jobs/Event Scheduler
问题描述
我是在MySQL和PHP / Cron Jobs的noob,所以如果你都可以很容易地解释它在哪里放事件等,这将是伟大的,无论如何,与问题。
I am quite the noob at MySQL and PHP/Cron Jobs so if you all could explain it easily as in where to put the event, etc, that'd be great, anyways, on with the question.
问题:我有一个名为my_Table的表,它有一个包含3个字段的条目:name,age,timestamp。所以一行有这个例子。 Sam | 20 | whateverthetimeis。
现在,当该行达到30分钟老,我希望它被删除,与任何其他行,30分钟,谢谢大家的帮助,我已经阅读其他答案,但像我说我' m仍然是一个初学者,不明白什么人在回答。感谢您的帮助
Question: I have a table called my_Table and it has an entry with 3 fields: name,age,timestamp. So one row has this for instance. Sam | 20 | whateverthetimeis. Now, when that row reaches 30 minutes old, I want it to be deleted, same with any other rows that hit 30 minutes, thank you all for the help, I have read the other answers but like I said I'm still a beginner and dont quite understand what people are answering. Thanks for the help
如果我可以用Cron作业或MySQL事件(我不知道如何设置一个事件),这将是伟大的任何人帮助! :D
If I can do this with a Cron Job or a MySQL Event (I don't know how to set an event up) it would be great for anyone to help! :D
我的代码在30分钟后删除(删除所有行,即使他们不是30分钟)
My code for deleting after 30 minutes (Deletes all rows even when they aren't 30 minutes old)
<?php
include_once('../config.php');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);
$runpurge = mysql_query ("DELETE FROM my_table WHERE timestamp < NOW() - INTERVAL 30 MINUTE");
?>
主显示代码:
$result = mysql_query ("SELECT * FROM cw_LFG ORDER BY `time` DESC ") or die(mysql_error);
while ($row=mysql_fetch_array($result)) {
echo "<tbody>";
echo "<tr><td>";
echo strip_tags($row['cname']);
echo "</td><td>";
echo $row['level'];
echo "</td><td>";
echo strip_tags($row['region']);
echo "</td><td width='100px'>";
echo timeElapsed(strtotime($row['time']));
echo "</td><td style='word-wrap: break-word'>";
echo strip_tags($row['comment']);
echo "</td></tr>";
echo "</tbody>";
}
echo "</table>";
$curtime = date('Y-m-d H:i:s');
$curtimestr = strtotime($curtime);
function timeElapsed($time) {
$elapsedTime = time() - $time;
if ($elapsedTime < 1) {
return 'Right now';
}
if ($elapsedTime < 60){
return '< 1 Minute ago';
}
推荐答案
p>
The SQL would be
DELETE FROM my_table
WHERE timestamp < NOW() - INTERVAL 30 MINUTE
编写一个执行此SQL的PHP脚本,并添加一个crontab条目每30分钟运行一次。或者使用MySQL事件计划程序定期运行它; 此处。
Write a PHP script that executes this SQL, and add a crontab entry that runs it every 30 minutes. Or use the MySQL Event Scheduler to run it periodically; it is described here.
这篇关于30分钟后使用Cron作业/事件计划程序删除MySQL行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!