使用php和mysql获取随机id


to get random id using php and mysql

我有两个表,其中包含id,subject,exam,qution,qution_id,在表1中在表2中id,question_id,answer,correct,这里我有一个问题,从数据库中得到一个随机问题我正在使用

$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question'  LIMIT 1");
    while($row = mysql_fetch_array($singleSQL)){
        $id = $row['id'];
        $thisQuestion = $row['question'];
        $type = $row['type'];
        $subject =$row['subject'];
        $exam =$row['exam'];
        $explan =$row['explan'];
        $question_id = $row['question_id'];
        $s ='<strong>'.$subject.'</strong>';
        $e ='<small>'.$exam.'</small>';
        $q = '<h2>'.$thisQuestion.'</h2>';
        $ex ='<p class="exp">'.$explan.'</p>';
        $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 .= '<table class="table table-hover table-bordered"> <tr>
            <td><label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label></td>
            </tr></table>
            <input type="hidden" id="qid" value="'.$id.'" name="qid"><br />
            ';
        }
        $output = ''.$s.','.$e.''.$q.','.$answers.''.$ex.' <span id="btnSpan"><button onclick="post_answer()" id="show">Submit</button></span>';
        echo $output;
       }
    }

当我点击提交答案时,我会随机得到下一个问题像

 index.php?question=

我在这里应该用什么当一个页面加载时,它应该从数据库中得到一个随机问题请帮我解决这个问题乙醇提前

也许你可以使用一个数组来存储问题id,并使用php的rand函数来生成一个随机数(它将在0和列表大小之间),这将是问题id列表的索引。

int rand ( int $min , int $max )
$randomparameter = mysql_query("SELECT MIN(id) as min_id,MAX(id) as max_id FROM questions ");
$row = mysql_fetch_array($randomparameter );
$random_questin_id=rand($row['min_id'],$row['min_id']);