简体   繁体   中英

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

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.

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

Also mysql_* is deprecated. Follow @Magnus's comment

Instead of using "ORDER BY RAND" in the query, grab all the possible questions. 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. Then all you need to do is use a for loop to get the required number of questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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