如何使用 php 或 cronjob 每天截断 table?

How to truncate table every day with php or cronjob?

我想在每个 day.I 时自动截断 table,我正在使用此方法并且它正在运行。

$truncate = "TRUNCATE TABLE myTable";

我正在使用提交按钮单击然后截断的代码 table 它工作得很好。

如何自动截断我的 table?我现在正在尝试使用 if-else 和 ajax

进行 strtotime

您只能使用 MySQL 创建此内容,阅读有关 MySQL Events

的更多信息

MySQL Events are tasks that run according to a schedule. Therefore, we sometimes refer to them as scheduled events. When you create an event, you are creating a named database object containing one or more SQL statements to be executed at one or more regular intervals, beginning and ending at a specific date and time. Conceptually, this is similar to the idea of the Unix crontab (also known as a “cron job”) or the Windows Task Scheduler.

例如:

CREATE EVENT IF NOT EXISTS my_truncater
  ON SCHEDULE AT CURRENT_TIMESTAMP
  DO
    TRUNCATE TABLE myTable

要定期重复您的活动,请使用 every 而不是 AT :-

To repeat actions at a regular interval, use an EVERY clause. The EVERY keyword is followed by an interval as described in the previous discussion of the AT keyword. (+ INTERVAL is not used with EVERY.) For example, EVERY 6 WEEK means “every six weeks”.

所以这个例子就像:-

CREATE EVENT IF NOT EXISTS my_truncater
  ON SCHEDULE EVERY 24 HOURS
  DO
    TRUNCATE TABLE myTable

如需更多 details/features ,请查看 MySQL.

Eventsman page for the syntax of the Events, also it's important to check out the right configurations

cronjob 方式:-

-- 创建一个可执行文件 [PHP file/SQL file ... etc] -- 假设您将选择 PHP,创建一个新文件 example.php

$query = "TRUNCATE TABLE myTable";

/* your mysqli/pdo connection & queries */

-- 将以下命令添加到 bash 脚本到 /etc/cron.daily 目录:

$ php /path/to/example.php

另一种解决方案是创建一个 cron 作业,它调用一个脚本来截断数据库中的 table。