Michael Naidis
Michael Naidis

Reputation: 124

Trying to load questions and answers from MySQL shows nothing

In my quiz system I am trying to actually now make the page of the questions, but it shows nothing when I try to show the question rows and the answer rows to the page.

<?php
    $q_qselect = mysql_query("SELECT * FROM `questions`");
    $q_qnumrows = mysql_num_rows($q_select);
    for($i=0;$i<$q_qnumrows;$i++){

        $q_qselect = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
        $q_aselect = mysql_query("SELECT * FROM `answers` WHERE `question_id`='$i'");

        $q = mysql_fetch_assoc($q_qselect);
        $a = mysql_fetch_assoc($q_aselect);

        echo $q['question'] . "<br />";
        echo $a['answer'] . "<br />";
    }
?>

And also, another question - how can I actually check that he selected the correct answer? (radio button near each answer) when the field in the answers table is correct?

Upvotes: 0

Views: 105

Answers (2)

Md. Maruf Hossain
Md. Maruf Hossain

Reputation: 922

You Can Try by using As Like Following....

<?php

  $query_result = mysql_query("SELECT questions.*, answers.* FROM questions LEFT JOIN answers on questions.id=answers.`question_id`");

  while($row = mysql_fetch_array($query_result)) {
    echo $row ['question'] . "<br />";
    echo $row ['answer'] . "<br />";
  }

?>

Upvotes: 0

Ryan Beaulieu
Ryan Beaulieu

Reputation: 453

<?php
$question = mysql_query("SELECT questions.*, answers.* FROM questions inner join answers on questions.qid=answers.id");

   while($row = mysql_fetch_array($question)) {
   echo $row['question_column_name_in_DB'].'<br />' .$row['answer_column_name_in_DB'].'<br />';
}

?>

change column names to their appropriate names. Please look into PDO's once you have gotten this to work.

Upvotes: 0

Related Questions