0

I'm having a lot of trouble wrapping my brain around how to do this.

I have a database table with player information in it (Name, socialmedia handle, photo). I have a module where I want to display one player in that module randomly. Now if I just use:

$pQuery = "SELECT * FROM `players` ORDER BY RAND() LIMIT 1";

It'll pick a random player every time the page is refreshed.

I want to write something where it will display a new random player once a day. No matter how many times you refresh the page (as long as it is the same day), it will always show that one player. As soon as 12midnight (in my time zone, of course) strikes, it queries the table again and displays a different random player.

I'm trying to understand if setting up a SQL Event is the solution, but I'm not experienced enough in MySQL to understand how those work. I've also read that maybe a Cronjob is the solution.

I've been googling for days now and haven't been able to find a solution that I can wrap my brain around, so I'm hoping someone can point me in the right direction.

Thank you in advance.

5
  • Official MySQL event scheduler page: dev.mysql.com/doc/refman/8.4/en/event-scheduler.html. E.g. how to create an event: dev.mysql.com/doc/refman/8.4/en/create-event.html. Commented May 2 at 15:01
  • 2
    Is it possible you could provide a seed ( RAND(seed) ) , where seed is equivalent to the current date, such as days since epoch, which effectively means you get the same RAND result for the whole day. Commented May 2 at 15:08
  • Assuming you want a different player for every visitor, you could use a cookie to save information (date + random player id). When the cookie is empty or the date is past, draw a new player id and store it in the cookie. In case you want everyone to see the same player, just store the information globally (either in a file or the database). Commented May 2 at 15:18
  • @Computable "which effectively means you get the same RAND result for the whole day" Note that you may end up selecting a new record in case new players are added to the database in the middle of the day. Commented May 2 at 15:39
  • Another option it to flip over to your OS's native cron. Run your RAND() query literally once when you want it and persist the ID to a static location. Commented May 2 at 16:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.