我有一个问题从表'问题'中选择问题,随机化它,只选择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;
}
}
,如果你有连基本的错误处理,你会被告知关于你的第一个查询中的语法错误。永远不要成功。总是检查错误:'mysql_query(...)或die(mysql_error());' –
'ORDER BY'在WHERE子句之后,而不是之前。 RT * M http://dev.mysql.com/doc/refman/5.7/en/select.html –
感谢您的关注,先生,看来我在输入查询时犯了一个粗心的错误,它会尽量避免它下次。 – user3286777