php随机函数调用相同的随机值

时间:2015-03-31 15:39:44

标签: 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'])) {


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>";

}

我想用

打电话给righ的答案
$display = mysql_query("SELECT * FROM quiz ");

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中获得相同的随机值,这显示了数据库中的所有问题我想只显示那些随机生成的问题。

0 个答案:

没有答案