简体   繁体   English

PHP-将随机查询结果保留24小时

[英]PHP - Keep randomized query results for 24 hours

I have a simple query that displays random results, however I'd like to be able to keep those same results for a 24 hour period per visitor so it doesn't randomize on every page refresh. 我有一个显示随机结果的简单查询,但是我希望每个访问者都能在24小时内保持相同的结果,因此它不会在每次页面刷新时随机出现。 Here's the query: 这是查询:

<?php
$rs1= mysql_query("
SELECT * FROM Table_1
ORDER BY RAND()
LIMIT 4
");
while ($row1= mysql_fetch_array($rs1)) { ?>

I gather I'll need to use the date somehow. 我知道我需要以某种方式使用日期。 I did figure out how to get a random number to show for 24 hours: 我确实弄清楚了如何获得一个随机数字显示24小时:

<?php
date_default_timezone_set('America/Los_Angeles');
mt_srand(date('Ymd'));
$number = mt_rand(50, 5000);
mt_srand();  //reset for other calls

echo $number;
?>

But I'm not sure how to make that work with my original query. 但是我不确定如何使它与原始查询一起使用。 Any ideas? 有任何想法吗? Or should I be creating a cookie rather than trying to do it with only php? 还是我应该创建一个cookie而不是仅使用php来做? How would I make a cookie work with it? 我将如何使用它?

This is a bit CPU intensive if you have to calculate over lot of rows, but do not uses cookies nor any special data to keep selection. 如果您必须计算大量行,但是不使用cookie或任何特殊数据来保留选择,则这会占用大量CPU。

select * from table order by md5(concat(id,curdate())) limit 4

Will show same order on every query on the same day. 将在同一天的每个查询上显示相同的顺序。 If want to be diferent based on the user doing the query but still the same on the day, you can add his id to the concat too. 如果希望根据用户进行查询而有所不同,但当天仍然相同,则也可以将其ID添加到concat中。

select * from table order by md5(concat(id,userID,curdate())) limit 4

Should be say, that results are no more random and can be predicted by someone that takes the job. 应该说,结果不再是随机的,可以由接任者预测。 You can add a seed too, to mitigate some way this effect. 您也可以添加种子,以减轻这种影响。

Pass the date seed that you are using into the query. 将您正在使用的日期种子传递到查询中。 MySQL's rand() function also takes a seed which would make your random results the same. MySQL的rand()函数还带有一个种子,该种子将使您的随机结果相同。

http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand

Here is an example using the mysqli extension since mysql is deprecated. 由于不建议使用mysql,因此以下示例使用mysqli扩展名。

<?php
date_default_timezone_set('America/Los_Angeles');
mt_srand(date('Ymd'));
$number = mt_rand(50, 5000);

$mysqli = new mysqli("localhost", "user", "password", "database");
$stmt = $mysqli->prepare("SELECT * FROM Table_1 ORDER BY RAND(?) LIMIT 4");
$stmt->bind_param('i', $number);
$stmt->execute();

You could also just do it all in the query 您也可以在查询中完成所有操作

SELECT * FROM Table_1 ORDER BY RAND(CAST(CONCAT(YEAR(), MONTH(), DAY()) AS UNSIGNED)) LIMIT 4

Or whatever seed you want to get from the date. 或者您想从日期中获取任何种子。

Save/cache the result to a file (using serialize()) and use the contents of this file to generate your page. 将结果保存/缓存到文件中(使用serialize()),然后使用该文件的内容来生成页面。 Then refresh this file every 24 hours as needed. 然后根据需要每24小时刷新一次该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM