-1

Ubuntu 8.04 LAMP で PHP(5.2.4) MySQL(5.0.51a) で MySQLi を使用しています。

関連する Db テーブルは次のとおりです。

Questions
+----+------------------------------------------------------------------------------------------------------------+
| id | question                                                                                                   |
+----+------------------------------------------------------------------------------------------------------------+
|  1 | What is the correct rate of flow (in millilitres per minute) for a 14<abbr title="Gauge">G</abbr> cannula? |
|  2 | Which of the following drugs is not an anaesthetic  induction agent?                                       | 
+----+------------------------------------------------------------------------------------------------------------+

answers
+----+--------------------------------------------------------+
| id | answer                                                 |
+----+--------------------------------------------------------+
|  1 | 344<abbr title="millilitres per minute">ml/min</abbr>. | 
|  2 | 205<abbr title="millilitres per minute">ml/min</abbr>. | 
|  3 | 98<abbr title="millilitres per minute">ml/min</abbr>.  | 
|  4 | 60<abbr title="millilitres per minute">ml/min</abbr>.  | 
|  5 | Thiopental sodium                                      | 
|  6 | Propofol                                               | 
|  7 | Etomidate                                              | 
|  8 | Domperidone                                            | 
+----+--------------------------------------------------------+

a_lookup (to associate questions with answers)
+------+------+
| q_id | a_id |
+------+------+
|    1 |    1 | 
|    1 |    2 | 
|    1 |    3 | 
|    1 |    4 | 
|    2 |    5 | 
|    2 |    6 | 
|    2 |    7 | 
|    2 |    8 | 
+------+------+

以下を使用して、MySQLi を使用して Db に接続しています (プレースホルダー ジェネリックを優先して、実際のユーザー名、パスワード、およびデータベース名を編集したことを念頭に置いてください)。

<?php

if (!isset($qtype) && !isset($submitted)) {

  /*
  =========================================================================
  = queries the database if no specific question-type selected  =
  =========================================================================
  */

  $mysqli = new mysqli("localhost", "username", "password", "db_name");
  if ($result = $mysqli->query(
    "SELECT questions.id as qid, 
      questions.question as question
    FROM
      questions;")) {

      while ($row = $result->fetch_object()) {
        $i = $row->qid;

        $q[$i][question] = $row->question;

        if ($answers = $mysqli->query(
          "SELECT answers.id, answers.answer FROM answers, a_lookup, questions WHERE answers.id=a_lookup.a_id AND '$i'=a_lookup.q_id;")) {

          while ($row = $answers->fetch_object()) {

            if (!isset($c)) {
              $c = 1;
            }
            else {
              $c = $c;
            }

            $q[$i][answers][$c] = $row->answer;

            $c++;
          }
        }
      }
    }

  $mysqli->close();
}
elseif (isset($qtype)) {

  // this part should hopefully be invoked when tags 'question-types' are supported

}

?>

これにより、期待どおりに質問が返されます。print_r は次のとおりです。

Array
(
    [1] => Array
        (
            [question] => What is the correct rate of flow (in millilitres per minute) for a 14G cannula?
            [answers] => Array
                (
                    [1] => 344ml/min.
                    [2] => 205ml/min.
                    [3] => 98ml/min.
                    [4] => 60ml/min.
                    [5] => 344ml/min.
                    [6] => 205ml/min.
                    [7] => 98ml/min.
                    [8] => 60ml/min.
                )

        )

    [2] => Array
        (
            [question] => Which of the following drugs is not an anaesthetic  induction agent?
            [answers] => Array
                (
                    [9] => Thiopental sodium
                    [10] => Propofol
                    [11] => Etomidate
                    [12] => Domperidone
                    [13] => Thiopental sodium
                    [14] => Propofol
                    [15] => Etomidate
                    [16] => Domperidone
                )

        )

予想していた 4 つではなく、8 つの結果になってしまったことに混乱しています。データベースについて何か知っている人なら誰でも予想するように、mysql ターミナル クライアントを使用するか、php の mysql-api を使用するかに関係なく、8 を取得します。

重複を作成するために何が間違っていますか?

4

2 に答える 2

1

2 番目の SQL クエリが質問テーブルのいずれかの質問 ID と一致するため、回答が重複します。

試す

 SELECT answers.id, answers.answer FROM answers, a_lookup, questions WHERE
 answers.id=a_lookup.a_id AND '$i'=a_lookup.q_id AND '$i' = questions.id;

代わりは

いかなる場合でも。FROM の複数のテーブルを追跡するのは簡単ではありません。代わりに JOIN を試してください。なぜなら、すべての結合でフィルターを指定する必要があるため、テーブル内のフィルターを忘れないためです。

于 2009-07-10T16:15:47.303 に答える
1

そのようなループでクエリを実行するのは非常に非効率的であるため、実際には実行しないでください。たとえば、1 つのクエリでデータをフェッチできる必要があります。

SELECT Questions.id asquestion_id, Questions.question, answers.id as answer_id, answers.answer
FROM Questions
INNER JOIN a_lookup ON (Questions.id = a_lookup.q_id)
INNER JOIN answers ON (a_lookup.a_id = answers.id)

その結果を同じ構造にソートするのは簡単で、はるかに高速です。

$result = array();
while ($row = $result->fetch_assoc()) {
    $questionId = $row['question_id'];
    if (!isset($result[$questionId])) {
        $result[$questionId] = array('question'=>$result['question'], 'answers' => array());
    }
    $result[$questionId]['answers'][] = $row['answer'];
}
于 2009-07-10T15:57:31.817 に答える