试图从MySQL加载问题和答案没有任何结果

时间:2013-01-21 14:52:00

标签: html mysql arrays for-loop mysql-num-rows

在我的测验系统中,我现在尝试制作问题的页面,但当我尝试向页面显示问题行和答案行时,它没有显示任何内容。

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

另外,另一个问题 - 我怎样才能真正检查他是否选择了正确的答案? (答案表中的字段为correct

时,(每个答案附近的单选按钮)

2 个答案:

答案 0 :(得分:0)

<?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 />';
}

?>

将列名更改为适当的名称。一旦你开始工作,请查看PDO。

答案 1 :(得分:0)

您可以尝试使用As Like Follow ....

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

?>