I am trying to get a selection from my database that will last all day (daily selection). I am using the following code:
$query = 'SELECT * FROM table ORDER BY rand() LIMIT 1
But, as you can see, this only gives me a random selection from the table, and every time I refresh the page, I get a new random selection. How can I make a choice to last all day?
Thanks in advance <3
I am trying to do this:
$query = "SELECT * FROM table ORDER BY rand(" . date("Ymd") . ") LIMIT 1";
But I get the following error: mysql_fetch_assoc (): the provided argument is not a valid MySQL result resource. This is the part that will break:
$results = mysql_query($query); while($line = mysql_fetch_assoc($results))
So ... it should look like this, right? (I mean choosing daily random choices?)
$dailyPick = 'SELECT * FROM table ORDER BY rand() LIMIT 1'; $cacheKey = 'dailyPick'. date('dmY'); if($cache->has($cacheKey)) { $dailyPick = $cache->get($cacheKey); } else { // hit database $dailyPick = $cache->save($cacheKey); }
I'm trying to do it now:
$dailyPick = 'SELECT * FROM table ORDER BY rand() LIMIT 1'; $cacheKey = 'dailyPick'. date('dmY'); if($cache->has($cacheKey)) { $dailyPick = $cache->get($cacheKey); } else { // hit database $dailyPick = $cache->save($cacheKey); }
However, it becomes a mistake for me that I use the has function for a non-object.
php mysql random
Izumi
source share