Php 要显示相同的随机值吗

Php 要显示相同的随机值吗,php,html,sql,Php,Html,Sql,我正在做一个在线测验,id为opt1-opt4,答案在数据库中 我使用函数调用这些问题 $con = mysql_connect("localhost", "ashu","ashua"); $db=mysql_select_db("quiz",$con) or die(mysql_error()); $display = mysql_query("SELECT * FROM quiz ORDER BY rand() LIMIT 2"); if (empty($_POST['submit'])

我正在做一个在线测验,id为opt1-opt4,答案在数据库中

我使用函数调用这些问题

$con = mysql_connect("localhost", "ashu","ashua");
$db=mysql_select_db("quiz",$con) or die(mysql_error());

$display = mysql_query("SELECT * FROM quiz ORDER BY rand() LIMIT 2");

if (empty($_POST['submit'])) {


echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
echo "<table border=0>";

while ($row = mysql_fetch_array($display)) {

$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$answer = $row["answer"];

echo "<tr><td colspan=3><br><b>Q:$question</b></td></tr>";
echo "<tr><td><input type=radio name=q$id value=\"$opt1\">$opt1 </td></tr>";
echo"<tr><td> <input type=radio name=q$id value=\"$opt2\">$opt2     </td></tr>";
echo "<tr><td><input type=radio name=q$id value=\"$opt3\">$opt3 </td></tr>";
echo "<tr><td><input type=radio name=q$id value=\"$opt4\">$opt4 </td></tr>";

}

echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";

}
现在我想为答案显示相同的随机值,但我无法调用相同的随机值。 答案的功能是

$display = mysql_query("SELECT * FROM quiz ORDER BY id");

while ($row = mysql_fetch_array($display))
{

$question = $row["question"];
$answer = $row["answer"];
 $q = $_POST["q$id"];

echo "<tr><td><br>$question</td></tr>";

if ($q == $answer) 
    {
    echo "<tr><td>&raquo;You answered $q, which is correct</td></tr>";
    }
elseif ($q == "") {
echo "<tr><td>&raquo;You didn't select an answer. The Correct answer is $answer</td>     </tr>";
}
else {
echo "<tr><td>&raquo;You answered $q. The Correct answer is $answer</td></tr>";
}

}
echo "</table></p>";



}
如何在php中获得相同的随机值
显示数据库中的所有问题我只想显示随机生成的问题。显示问题的精确而简短的解决方案是将所有问题存储在一个数组中,并使用数组shuffle和array_rand函数对数组进行随机排列或随机排列

下面是一个洗牌的例子,您可以在您的案例中使用

在while{}循环中,将所有$question=$row[question]放在一个数组中

$randomize_questions = array($question);

shuffle($randomize_questions);

print_r($randomize_questions); //this will put all the questions in a random order

然后您可以从$randomize\u questions

中回显值,$row=mysql\u fetch\u array$display有两个while循环,我应该在其中放置这些值。我希望在下一个while循环中使用相同的随机值。是否可能