Php 随机、查询、限制
我有一个问题要从“问题”表中选择问题,将其随机,只选择15个问题。我提出了以下问题,但并不像我希望的那样有效Php 随机、查询、限制,php,mysql,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']; $thisQue
$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;
}
}
$singleSQL=mysql\u查询(“从问题中选择*,其中id='$question'ORDER BY RAND()LIMIT 1”);
while($row=mysql\u fetch\u数组($singleSQL))
{
$id=$row['id'];
$thisQuestion=$row['question'];
$type=$row['type'];
$question_id=$row['question_id'];
$q=“.$thisQuestion.”;
$sql2=mysql\u查询(“从答案中选择*,其中问题id='$QUEST'按兰德排序()”;
while($row2=mysql\u fetch\u数组($sql2))
{
$answer=$row2['answer'];
$correct=$row2['correct'];
$answers.=''.$answer
';
}
$output='.$q.','.$answers.',提交';
echo$输出;
}
}
替换为:
"SELECT * FROM questions WHERE id='$question' ORDER BY RAND() LIMIT 1"
在我替换为您共享的查询后,表中的问题不能随机化,我想知道它哪里出错了…它只从表中为特定问题id选择一条记录。因此,表中可能只有一条记录具有该问题id。我理解它与表中id匹配的部分,但这仍然让我感到困惑,“order by rand()”应该运行得很好,但它仍然没有显示我想要的任何结果,我在输入“order by rand()”之前的第一个查询是mysql\U查询(“从id='$QUOTE'LIMIT 1')的问题中选择*;如果您甚至有基本的错误处理,您在第一次查询中就会被告知语法错误。永远不要假设成功。始终检查错误:
mysql\u query(…)或die(mysql\u error());
ORDER BY
在WHERE
子句之后,而不是之前。谢谢您的关心,先生,似乎我在键入查询时犯了一个粗心的错误,下次它会尽量避免。@Fred ii-我也这样说,只有在我的回答中,ORDER BY rand
在WHERE
子句之后。
"SELECT * FROM questions WHERE id='$question' ORDER BY RAND() LIMIT 1"