skip to Main Content

I have a website that let users download PDF files,
I made a download limit 10 files per day for each user and I stored the numbers inside MySQL database. everything works great!

The problem is: The limit will be set forever, I want it to reset to 0 each 24h.
Is there any phpmyadmin command or something to reset that column each 24h to 0 automatically or any other way!

2

Answers


  1. I would try a different way.

    I would add table download_log(user_id, file_id, timestamp) and insert row every time user downloads some file.
    To check if user reached limit you can check how many downloads he did perform in last 24h.
    Something like

    SELECT COUNT(*) FROM download_log WHERE user_id = ? AND timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
    

    To schedule some operations you can use cron to run script

    Login or Signup to reply.
  2. It looks like you are already storing the download counts in a log table of sorts.

    For expiring those counts, I suggest using the event scheduler native to MySQL. You may need to enable it in your server parameters.

    Syntax would look something like this:

    CREATE EVENT myevent
        ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
        DO
          UPDATE count_db.count_table SET count_column = 0;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search