I have website about dog breeds which has a 'dog of the day' section. I'd like 'dog of the day' to display a new dog (from a mysql database) once a day. Using php and mysql, I'd imagine the operation is actually quite simple.
$connection = mysqli_connect("localhost", "username", "password");
$randomdog = mysqli_query($connection, "SELECT * FROM dogtable WHERE id >= RAND() * (SELECT MAX(id) FROM dogtable) LIMIT 1"));
echo $randomdog
Of course, this gives me a random dog every-time the page is reloaded. I want $randomdog to only change once a day.
Would a cron job be the answer? I've never implemented one before, but I have cpanel installed on my site, so I don't think it would be too hairy.
If I was to use cron job, I'm assuming I'd save
$connection = mysqli_connect("localhost", "username", "password");
$randomdog = mysqli_query($connection, "SELECT * FROM dogtable WHERE id >= RAND() * (SELECT MAX(id) FROM dogtable) LIMIT 1"));
as a .php file to be run once a day by the cron job, updating $randomdog once a day. But then how could I pass $randomdog to a separate page to be echoed?
I've asked a similar question here before, and the answers usually go along the lines of "store a unixtimestamp, at a week to it, execute again when current time == time + 1 week".
That's impossible with php? It's only executed when a user requests it, surely? Meaning the 'time + 1 week' variable cannot possibly be stored somewhere.
Anyway, by all means correct me.