Search code examples
phpmysqldatetimeauto-update

How to update automatically data when finished day (00:00) in mysql


I am developing a project by PHP and Mysql (PhpMyadmin), so I would like to update data automatically when finished day (00:00) in mysql.

Any ideas?


Solution

  • You have to use MySql-events.

    Use below code for executing daily at 00:00:

    CREATE EVENT my_event
      ON SCHEDULE
        EVERY 1 DAY
        STARTS '2014-04-30 00:00:00' ON COMPLETION PRESERVE ENABLE 
      DO
        # My query
    

    Use below code for executing only once at 00:00:

    CREATE EVENT my_event
        ON SCHEDULE
      AT ('2017-03-14 00:00:00'+ INTERVAL 1 DAY) ON COMPLETION PRESERVE ENABLE 
        DO
      # My query
    

    Also, read the documentation on CREATE EVENT.