简体   繁体   English

在php在线考试系统中由rand使用订单时重复出现问题

[英]Questions are repeating when used order by rand in php online exam system

Questions are repeating when used order by rand in php online exam system 在php在线考试系统中由rand使用订单时重复出现问题

if(isset($_SESSION['stdname'])){
    $result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
    $r1=mysql_fetch_array($result);
    $result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn']."order by rand();");
    $r=mysql_fetch_array($result);

Use the key word DISTINCT in your query. 在查询中使用关键字DISTINCT

$result=executeQuery("select DISTINCT * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn']." order by rand();");

Also mysql_* is deprecated. mysql_ *也已弃用。 Follow @Magnus's comment 关注@Magnus的评论

Instead of using "ORDER BY RAND" in the query, grab all the possible questions. 代替在查询中使用“ ORDER BY RAND”,抓住所有可能的问题。 Once you've got the questions into an array, use the shuffle() function ( http://php.net/manual/en/function.shuffle.php ) to randomise the questions. 将问题放入数组后,请使用shuffle()函数( http://php.net/manual/en/function.shuffle.php )将问题随机化。 Then all you need to do is use a for loop to get the required number of questions. 然后,您所需要做的就是使用for循环来获取所需数量的问题。

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

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