0

在我的测验系统中,我实际上正在尝试制作问题页面,但是当我尝试在页面上显示问题行和答案行时,它什么也没显示。

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

4

2 回答 2

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。

于 2013-01-21T15:14:37.517 回答
0

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

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

?>
于 2013-01-22T10:55:10.973 回答