0

我在下面有一个带有动态 where 子句的查询,它根据所选的下拉菜单输出结果。现在 where 子句可以根据所选选项正确添加和删除某些条件。

但是我有点意识到,如果我想查看所有问题,它没有很好的QuestionId = ?条件,但是当它假设显示每个问题的答案时,而不是像这样显示它:

D (this is for question 1)
B,C,E (this is for question 2)

它将它们全部显示为一个答案,如下所示,这是不正确的:

D,B,C,E

我的问题是导致这种情况的原因是查询还是循环?

var_dump($questions): 显示:

array(1) { [39]=> array(1) { [72]=> array(1) { ["answer"]=> string(7) "B,C,D,E" } } }

72 是 QuestionId,所以出于某种奇怪的原因,它没有解决所有问题(应该有第二个问题,即 73)

下面是代码:

    $selectedstudentanswerqry = "
            SELECT sa.StudentId, q.QuestionId,
            GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer
            FROM Student st
            INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
            INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
            INNER JOIN Answer an ON q.QuestionId = an.QuestionId
            ";

            // Initially empty
            $where = array('q.SessionId = ?');
            $parameters = array($_POST["session"]);
            $parameterTypes = 'i';


            //check if POST is empty

            // Check whether a specific student was selected
            $p_student = empty($_POST["student"])?'':$_POST["student"];

            switch($p_student){
            case 0:
                //dont' add where filters
                break;
            default:
                $where[] = 'sa.StudentId = ?';
                $parameters[] .= $_POST["student"];
                $parameterTypes .= 'i';
            }

            // Check whether a specific question was selected
            $p_question = empty($_POST["question"])?'':$_POST["question"];

            switch($p_question){
            case 0:
                //dont' add where filters
                break;
            default:
                $where[] = 'q.QuestionId = ?';
                $parameters[] .= $_POST["question"];
                $parameterTypes .= 'i';
            }

            // If we added to $where in any of the conditionals, we need a WHERE clause in
            // our query
            if(!empty($where)) {
                $selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
                global $mysqli;
                $selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
                // You only need to call bind_param once

                if (count($where) == 1) {
                $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
            }
            else if (count($where) == 2) {
                $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
            }
            else if (count($where) == 3) {
                $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
            }

            }

            $selectedstudentanswerqry .= "
              GROUP BY sa.StudentId, q.QuestionId
              ORDER BY StudentAlias, q.SessionId
            ";

        // get result and assign variables (prefix with db)
        $selectedstudentanswerstmt->execute(); 
        $selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsQuestionId,$detailsAnswer);   

        $selectedstudentanswerstmt->store_result();
        $selectedstudentanswernum = $selectedstudentanswerstmt->num_rows(); 

    $questions = array();

    while ($selectedstudentanswerstmt->fetch()) {

        $questions[$detailsStudentId][$detailsQuestionId] = array(

        'answer'=>$detailsAnswer,

);

}

...

<?php   

var_dump($questions);

        foreach ($questions as $studentId => $student)
{

foreach ($student as $questionId => $question) {

echo '<p><strong>Answer:</strong> ' .htmlspecialchars($question['answer']).  '</p>' . PHP_EOL;

}

}

?>
4

1 回答 1

0

看起来问题实际上是:

GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer

查询的一部分。如果没有看到实际的表结构和数据,就很难在脑海中形成正在发生的事情。“答案”可能是一个模棱两可的列,因此会导致不可预知的结果。

您可能会考虑为此编写子查询或创建视图。

SELECT sa.StudentID, q.QuestionId, sa.Student_Answers
FROM (
     SELECT GROUP_CONCAT(DISTINCT a.Answer SEPERATOR ',') as Question_Answers
     FROM Student_Answer a
     GROUP BY a.QuestionID
) AS sa
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)

查看您的代码,似乎不需要加入学生表,因为他们的答案中已经存在 ID。尽管我确信在某些时候您会将名称与 ID 相关联。

于 2013-02-08T04:59:55.210 回答