Join + While循环只输出一个结果

时间:2012-11-27 16:03:36

标签: php mysql join while-loop

我正在尝试进行测验,并使用以下内容来比较结果,它似乎工作和输出一次,但就是这样,我不会再处理任何记录,即使有两个问题在测验,所以它应该输出正确,正确或正确,不正确等。

          <?php
    // Make a MySQL Connection
    // Construct our join query
    $query = "SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions
    q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and
    a.quizselectanswer = q.correctanswer" or die("MySQL ERROR: ".mysql_error());

    $result = mysql_query($query) or die(mysql_error());


    // Print out the contents of each row into a table 
    while($row = mysql_fetch_array($result)){
        if ($row['correctanswer'] == $row['quizselectanswer']){
            echo 'CORRECT';}
            else { echo 'INCORRECT';
            }

        echo "<br />";
    }
    ?>

编辑&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT; &GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;

表结构如下&gt;&gt;

itsnb_chronoforms_data_answerquiz cf_id,cf_uid,cf_created,cf_modified,cf_ipaddress,cf_user_id,questionID,quizselectanswer,quizID,userID

itsnb_chronoforms_data_createquestions cf_id,cf_uid,cf_created,cf_modified,cf_ipaddress,cf_user_id,quizID,questionID,quizquestion,quizanswer1,quizanswer2,quizanswer3,quizanswer4,questionformat,correctanswer

1 个答案:

答案 0 :(得分:2)

当你删除selectanswer = correctanswer条件时,你得到四个答案(这确实不是你想要的,因为你想要获取正确和错误的答案),因为你没有关联答案ID。

$query = "SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions
q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and
a.quizselectanswer = q.correctanswer" or die("MySQL ERROR: ".mysql_error());

应该是:

$query = "SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions
q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and
a.questionID = q.questionID" or die("MySQL ERROR: ".mysql_error());

你在没有这个条件的情况下得到了questionID的交叉产品。您看到的四条记录(如果questionID为1和2):

  1. a.questionID = 1且q.questionID = 1
  2. a.questionID = 1且q.questionID = 2(不要这样)
  3. a.questionID = 2且q.questionID = 1(或此)
  4. a.questionID = 2且q.questionID = 2