-1

我在 Ubuntu 8.04 LAMP 上使用 MySQLi 和 PHP(5.2.4) MySQL(5.0.51a)。

相关的 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
                )

        )

我只是对最终得到八个结果而不是我期望的四个结果感到困惑。我得到了八个——因为任何了解数据库的人都会期望我使用 mysql 终端客户端还是 php 的 mysql-api。

创建重复项我做错了什么?

4

2 回答 2

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 回答
1

您会得到重复的答案,因为您的第二个 sql 查询与 questions 表中的任一问题 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 回答