如何从mySQL和PHP中将结果检索为多维数组? [英] How do I retrieve results as multidimensional array from mySQL and PHP?
问题描述
我刚刚开始学习与PHP一起使用的更高级的SQL,而且我真的很努力地尝试找出如何查询数据库以进行我正在建立的测验.
I'm just starting to learn more advanced SQL along with PHP and I'm really struggling trying to find out how to query my database for a quiz I'm building.
最终,我试图返回具有以下结构的json对象,该对象以多维数组的形式给出问题列表和所有可能的答案:
{
"questions":
[
{
"question": "question text here",
"answers":
[
{ "answer": "answer text here", "points": 10 },
{ "answer": "answer text here", "points": 20 },
{ "answer": "answer text here", "points": 30 },
{ "answer": "answer text here", "points": 40 }
]
},
{
"question": "question text here",
"answers":
[
{ "answer": "answer text here", "points": 10 },
{ "answer": "answer text here", "points": 20 },
{ "answer": "answer text here", "points": 30 },
{ "answer": "answer text here", "points": 40 }
]
}
]
{
...从具有以下结构的mySQL表中:
测验
id | title
1 | quiz title here
quiz_question
quiz_question
id | quiz_id (FK) | question_text
1 | 1 | question text here
2 | 1 | question text here
quiz_answer
quiz_answer
id | quiz_question_id (FK) | answer_text | points
1 | 1 | answer text here | 10
2 | 1 | answer text here | 20
3 | 1 | answer text here | 30
4 | 1 | answer text here | 40
...具有以下外键:
quiz_question.quiz_id is FK to quiz.id
quiz_answer.quiz_question_id is FK to quiz_question.quiz_id
...使用以下PHP(以最简单的形式,目前仅返回我的问题):
//query the db
$query = mysql_query("
SELECT quiz_question.question_text
FROM quiz_question
JOIN quiz ON quiz.id = quiz_question.quiz_id
WHERE quiz.id = 1;
");
$numrows = mysql_num_rows($query);
for ($i = 0; $i < $numrows; $i++) {
$row = mysql_fetch_assoc($query);
$quiz_data[$i] = array("question" => $row["question_text"]);
}
//echo JSON to page
$response = $_GET["jsoncallback"] . "(" . json_encode($quiz_data) . ")";
echo $response;
...并在我的JavaScript中使用jQuery的$ .getJSON(),这会从我的PHP获取一个JSON格式的对象,这使我返回以下内容:
[
{"question":"question text here"},
{"question":"question text here"}
]
所以我的问题是,如何编写我的SQL和PHP以创建像上面的多维数组,而不是像现在返回的单个数组?我需要弄清楚如何将问题和所有相关的答案作为多维数组来包含.
So my question is, how can I write my SQL and PHP to create a multidimensional array like the very above instead of a single array like I'm currently getting back now? I need to figure out how to include the questions and all associated answers as a multidimensional array.
推荐答案
您不能仅使用mysql检索多维数组(至少据我所知).您将必须进行一些php处理.这听起来并不疯狂.
You can't retrieve a multi-dimensional array purely with mysql (at least as far as I know). You will have to do some php processing. This doesn't sound too crazy.
首先,通过使用问题ID在quiz_questions
上加入quiz_answers
来更新查询以同时选择答案.然后,在您的循环中:
First, update your query to select answers at the same time by joining quiz_answers
on quiz_questions
using the question ID. Then, in your loop:
$quiz = array();
while ($row = mysql_fetch_assoc($result)) {
// you don't need to check num_rows
// fetch_assoc returns false after the last row, so you can do this
// which is cleaner
if (!isset($quiz[$row['question_id'])) {
$quiz[$row['question_id']] = array(
'question' => $row['question_text']
, 'answers' => array()
);
}
$quiz[$row['question_id']]['answers'][] = $row['answer_text'];
}
$full = json_encode(array('questions' => $quiz'));
这将为您提供经过json编码后的所需数组.
This will give you the array you want after it's json encoded.
请注意,您最终将每个答案选择一次问题文本/ID,这效率低下.您可以在答案上使用GROUP_CONCAT
,但以上内容仍然几乎可以正常使用,只需拆分答案字符串即可.
Note that you will end up selecting the question text/id once per each answer, which is inefficient. You can use GROUP_CONCAT
on the answers, but the above will still work almost identically, you just have to split the answer string.
我还建议您在mysql_*
上使用PDO
或其他包装器.
I also suggest you use PDO
or some other wrapper over mysql_*
.
这篇关于如何从mySQL和PHP中将结果检索为多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!