6

I'm trying to get a pick from my DB that would last for a day (daily pick). I use the following code:

$query = 'SELECT * FROM table ORDER BY rand() LIMIT 1

But as you can see it only gives me a random pick from the table, and every time I refresh the page it gets me a new random pick. How can I make the pick to last for a whole day?

Thanks in advance <3


I'm trying this:

$query = "SELECT * FROM table ORDER BY rand(" . date("Ymd") . ") LIMIT 1";

But I get the following error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource. This is the part that gets broken:

$results = mysql_query($query); 

while($line = mysql_fetch_assoc($results)) 

So... it should look like this, right? (I mean, choosing the daily random pick?)

$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 this 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 gets me a mistake that I'm using the 'has' function on a non-object.

4 Answers 4

15

If you set the SEED for the rand to an integer value that changes daily, that would solve your problem

$query = "SELECT * FROM table ORDER BY rand(" . date("Ymd") . ") LIMIT 1";

Would do the trick.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 Dude, I had no idea RAND() accepted arguments, this is a brilliant idea!
@Izumi: Try $query = "SELECT * FROM table ORDER BY rand(" . date('Ymd') . ") LIMIT 1;";
What do you mean by 'expensive' ?
by expensive Jacco means, it takes more power from your system than other, perhaps leener approaches.
Can anyone explain to me why the codes get broken and it says that the fetch is not working properly...
|
5

A sane means of doing this would be to automatically generate the pick of the day content via a cron job that was setup to run once a day.

As such, the cron job would execute the SQL you provided and store the appropriate content in a flat file/database table, etc. (or perhaps even just store the choosen id in another table for future lookup purposes).

1 Comment

This is the best approach. The random selection is done only once. after this, showing your daily pick is a fast and simple select query.
2

You can try something like this:

$total = 'SELECT COUNT(*) FROM table;';
$query = 'SELECT * FROM table ORDER BY id ASC LIMIT 1 OFFSET ' . (date('Ymd') % $total) . ';';

1 Comment

Gnutt's solution is much more elegant!
0

I think you'll need to update the random picked record with "today" field = 1..

Something like this:

// ------------
// Run this 3 commands once a day

// Reset all records
mysql_query("UPDATE `table` SET `today` = 0");

// Pick one
$sql = mysql_query("SELECT `id` FROM `table` ORDER BY RAND() LIMIT 1");
$id = mysql_result($sql, 0, 'id');

// Update the record
mysql_query("UPDATE `table` SET `today` = 1 WHERE `id` = {$id}");

// ------------

// Now you can find again your "random found record":
$query = mysql_query("SELECT * FROM `table` WHERE `today` = 1");

1 Comment

I just don't understand what do you mean by 'running them once a day'... and won't this delete my table? or it creates a new table called 'today'? I'm sorry... I just don't understand... sighs and looks down

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.