gpt4 book ai didi

PHP 随机查询

转载 作者:行者123 更新时间:2023-11-27 23:34:55 25 4
gpt4 key购买 nike

我已经成功地随机化了我的测验,但我现在面临的问题是,即使已经回答了某些问题,也会再次显示。我还有一个限制将从数据库中检索的问题的问题。我想让问题在测验中只随机显示一次,并且只从数据库中选择 5 个问题作为示例。

$singleSql = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 1");    
while ($row = mysql_fetch_array($singleSql))
{
$id = $row['id'];
$thisQuestion = $row['question'];
$question_id = $row['question_id'];
$q = '<h2>'.$thisQuestion.'</h2>';


$sql2 = mysql_query("SELECT * FROM answers where question_id = '$question_id' 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 />';
}
}

最佳答案

使用join。不需要两个不同的查询

SELECT * FROM questions q join answers a
on q.question_id=a.question_id
ORDER BY RAND() LIMIT 1

关于PHP 随机查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com