简体   繁体   中英

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 ?

<?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.

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 />";
  }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM