14

I have no idea where to start with this one:

I have a database that stores postID and 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.

I've been looking all over the place for something that does this and I've found absolutely nothing.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Sweepster
  • 1,829
  • 4
  • 27
  • 66
  • There are a number of aspects to this: (1) writing the `DELETE` statement; (2) creating a script that runs the `DELETE` statement; (3) setting up the script to run nightly at midnight. Which of those do you need help with? – ruakh Mar 26 '12 at 00:56
  • I needed help to figure out how to get my code to fire automatically without user input. The answers below all pointed to CRON so i'll look into that. – Sweepster Mar 26 '12 at 01:57

6 Answers6

20

You can use PHP script and use cron job on your cpanel.

Example:

cronjobcommand.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.

enter image description here

hodl
  • 1,420
  • 12
  • 21
4

For those out there who are on a shared hosting, like 1and1's, and can't use cron, here are 2 alternatives :

  • mysql events enable 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

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

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
3

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:

SELECT * FROM `posts`
WHERE (DATE(`post_date`) = DATE(NOW()));

This way you're not required to 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 time using by mysql command like:

DELETE FROM `posts` WHERE (
    DATE(`post_date`) < DATE(NOW())
)
Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
  • Suppose, i have case in that, in my databse I have record which had been posted on perticular date and need to delete on some particular deadline date. what you will do to run script automaticaly? – sudhakar phad Jul 29 '16 at 15:11
  • @sudhakarphad Sorry for my late response; you can just add deadline time to your comparition (post_date + deadlinetime) in that case. – Mojtaba Rezaeian Aug 28 '16 at 07:08
2

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.

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.

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:

# 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.

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
0

You should set cron job (scheduled tack.) for it.

A cron job is 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.

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.

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.

-4

MySQL doesn't have a task scheduler. So you have to use the task scheduler of your Operating System (CRON under 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).

Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • 4
    That's not true. MySQL has an Event Scheduler, implemented in version 5.1.6 (2006-02-01). More info: [MySQL Event Scheduler](http://dev.mysql.com/doc/refman/5.1/en/events.html). – devrique Aug 24 '14 at 15:58
  • MySQL has Event Scheduler – asvignesh Jun 14 '18 at 07:12