随机,查询,限制

时间:2015-12-15 19:01:02

标签: php mysql

我有问题从表'问题'中选择问题,随机化它,并且只选择15个问题。我提出了以下查询,但这种查询并不像我希望的那样。

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

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

1 个答案:

答案 0 :(得分:-2)

"SELECT * FROM questions ORDER BY RAND(), WHERE id='$question' LIMIT 1"

替换为:

"SELECT * FROM questions  WHERE id='$question' ORDER BY RAND() LIMIT 1"