PHP不重复上次获取
我有以下代码:PHP不重复上次获取,php,mysql,fetch,Php,Mysql,Fetch,我有以下代码: $result = mysql_query("SELECT * FROM quote ORDER BY RAND() LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array( $result ); echo $row['frase']; 每次你查询时,它都是随机的“磨损” 我想做的是避免在连续查询中出现相同的结果 我想说清楚,如果我的数据库中有: 1 a 2 b 3 c 如果来自echo$row[
$result = mysql_query("SELECT * FROM quote ORDER BY RAND() LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array( $result );
echo $row['frase'];
每次你查询时,它都是随机的“磨损”
我想做的是避免在连续查询中出现相同的结果
我想说清楚,如果我的数据库中有:
1 a
2 b
3 c
如果来自echo$row['frase']
我得到了a
下一个结果不能是a
我希望我说得很清楚,如果没有,请发表评论,我将进一步阐述
提前谢谢
顺便说一句:对不起@deceze@zerkms@Dr.Molle,因为最后一个问题完全是一团糟!没有伤害,只是狂欢中的一个noob:)
编辑:
为了回答Jason McCreary,我实际上这样称呼它:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
函数loadXMLDoc()
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“ajax_info.php”,true);
xmlhttp.send();
}
让AJAX更改此文本
更改内容
由于最后一个查询需要在页面刷新期间保持,因此您需要使用会话。大致如下:
session_start();
if (empty($_SESSION['lastresult'])) {
$_SESSION['lastresult'] = null;
}
$query = "SELECT * FROM `quote` WHERE `id` != '%s' ORDER BY RAND() LIMIT 1";
$query = sprintf($query, mysql_real_escape_string($_SESSION['lastresult']));
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$_SESSION['lastresult'] = $row['id'];
echo $row['frase'];
您可能希望将最后的x结果存储在会话中,并在查询中使用
NOT in()
,以避免每秒钟(第三、第四、…)的引号都相同。此查询是在页面重新加载时使用还是稍后在脚本中使用?我编辑了我的问题。现在是齐尔吗?谢谢谢谢deceze,我正在尝试!就您正在做的事情而言,您应该避免使用ORDER BY RAND(),而是计算一个介于0和结果数(或等效值)之间的随机位置,并在应用程序层中对其进行洗牌,以避免mysql使整个数据集随机化,然后在查询中使用该值,要求第一个条目大于或小于该随机值。在mysql上更好,您可以在会话中持久化数据,因此您可以加快创建有用的随机位置,以便在mysql中进行选择。