0

I have a 2 tables which contain id,subject,exam,qution,qution_id, in table 1 in table 2 id,question_id,answer,correct, here i have a problem to get a random questions from data base i am using

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

when i click submit answer i should get a next question randomly like

 index.php?question=

what should i use here when ever a page loads it should get a random question from database plz help iam in need of this thanx in advance

Pablo
  • 5,897
  • 7
  • 34
  • 51
learner
  • 31
  • 4
  • possible duplicate of [MySQL select 10 random rows from 600K rows fast](http://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast) – Anthony Aug 01 '13 at 17:00
  • mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the [MySQLi](http://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](http://www.php.net/manual/en/ref.pdo-mysql.php) extension should be used. – bansi Aug 01 '13 at 17:00
  • You have to return 4th row Luke. http://xkcd.com/221/ – Rakesh Juyal Aug 01 '13 at 17:38
  • Can't you just add ORDER BY RAND() to your first query, just like you are doing in the second query? – BigToach Aug 05 '13 at 05:28

2 Answers2

0

Perhaps you could use an array to store the question id's, and use php's rand function to generate a random number (which will be between 0 and the size of the list), which would be the index of the list of question id's.

int rand ( int $min , int $max )
Stu Whyte
  • 758
  • 5
  • 19
  • sir i have created an array with an session plz check the code tel me where i have to use rand eliment $sql = mysql_query("SELECT id FROM questions"); $numQuestions = mysql_num_rows($sql); if(!isset($_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){ $currQuestion = "1"; }else{ $arrCount = count($_SESSION['answer_array']); – learner Aug 02 '13 at 13:15
0
$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']);
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41