Я использую MySQLi с PHP (5.2.4) MySQL (5.0.51a) в лампе Ubuntu 8.04.
Соответствующие таблицы БД приведены ниже:
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, используя следующее (имея в виду, что я только что отредактировал настоящее имя пользователя, пароль и имя базы данных в пользу шаблонов-заполнителей):
<?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 или mysql-api php.
Что я делаю не так, чтобы создать дубликаты?