dsgdhtr_43654 2015-03-03 06:46 采纳率: 100%
浏览 12

显示随机问题

I am designing a test where in I want to display questions randomly. I have 50 questions in the database. For this I have written the following code :

$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' ORDER BY Rand()"); 

        while($row = mysql_fetch_array($singleSQL)){
            $id = $row['id'];
            $thisQuestion = $row['question'];
            $type = $row['type'];
            $question_id = $row['question_id'];
            $q = '<h2>'.$thisQuestion.'</h2>';
            $sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
            while($row2 = mysql_fetch_array($sql2)){
                $answer = $row2['answer'];
                $correct = $row2['correct'];
                $answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label> 
                <input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
                ';

            }
            $output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
            echo $output;
           }

This rand() function in the first line is not working for questions but the same function is working for answers(options of MCQs appears randomly). Also when I do changes in sql query in the first line I get the error :

Warning:

mysql_fetch_array() expects parameter 1 to be resource
boolean given in C:\xampp\htdocs\questions.php on line 36
undefined.

I dont know what's wrong!! Pls help me out..!!

  • 写回答

2条回答 默认 最新

  • doushan3511 2015-03-03 06:56
    关注

    The WHERE id = '$question' clause in the first query is making it return just a single question that matches that ID, not all the questions. You need to remove that.

    $singleSQL = mysql_query("SELECT * FROM questions ORDER BY Rand()"); 
    

    And inside the loop, you're using the wrong variable in the second query. It should be:

    $sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question_id' ORDER BY rand()");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了