empty the records of a mysql table with event_scheduler 24h


I want to make every 24 hours vacíen the records of a table from the web. what I wear in javascript is this is not much but is more or less see the idea

var now = new Date(); var hours = now.getHours();

if ((hours>=6)&&(hours<21)) txthora="se vacian registro";



Since many advised me to use the event_scheduler e created this event but I do not empty the tables. I already tried it for 24 hours

asked by steven 16.08.2018 в 00:16

2 answers


You can do it from the console for example like this

DROP EVENT 'limpieza'; CREATE DEFINER='root'@'localhost' EVENT 'limpieza' ON SCHEDULE EVERY 1 DAY STARTS '2018-08-18 00:00:00' ON COMPLETION PRESERVE ENABLE DO TRUNCATE TABLE 'tabla que quieres que se vacie '
answered by 21.08.2018 / 16:13

Maybe you can run it using ajax (if that's what you're looking for) like this.


Remember that your now.getHours(); returns a number between 0 and 23, if you want it to be every 24 hours, you must apply the logic as if (hours == 6){} since, this way you establish that the record is emptied only at a specific time (in this case it is 6, it may well be this time 0). It should also be noted that in the file php id is the name of the column , in this case we would delete the row with name "record". If what we want is to eliminate the records of the complete table in borrar.php , where is over, being as follows delete from $tabla_db1;


In JavaScript.

var now = new Date(); 

var hours = now.getHours();

if ( hours >= 6 && hours < 21){

       url: "borrar.php",
       method: "POST",
       async: false,
       dataType: "json",
       success: function(respuesta) {
         var txthora="se vacian registro";

Now, here's the php.

     // Actualizamos en función del nombre que tenemos

     $id = 'registro'; 


     $query = "delete from $tabla_db1 where id = '$id'";  
     $result = mysql_query($query);  


answered by 16.08.2018 в 03:16