I am designing a test where in I want to display questions randomly. I have 50 questions in the database. For this I have written the following code :
$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' ORDER BY Rand()");
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;
}
This rand() function in the first line is not working for questions but the same function is working for answers(options of MCQs appears randomly). Also when I do changes in sql query in the first line I get the error :
Warning:
mysql_fetch_array() expects parameter 1 to be resource
boolean given in C:\xampp\htdocs\questions.php on line 36
undefined.
I dont know what's wrong!! Pls help me out..!!