使用json_encode从MySQL查询返回JSON对象

时间:2014-02-28 20:04:59

标签: php mysql json

问题很长,所以我不得不缩短它。

无论如何,我目前的下表有以下结果。

我正在做的是以下内容:

  1. 查询与一个问题相关的所有答案
  2. 将其存储到数组后对其进行编码
  3. 这是我目前的疑问:

    $stmt = "SELECT questions.question_text, answers.answer_text 
       FROM     questions, answers, test
       WHERE    questions.question_id = answers.question_id
       AND      questions.test_id =1";
    
    $result = $connection->query($stmt);
    

    这给了我这个:

    enter image description here

    这是PHP:

    $encode = array();
    
    while($row = mysqli_fetch_assoc($result)) {
       $encode[] = $row;
    }
    
    echo json_encode($encode);  
    

    这给了我这个输出:

    [
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is a Hypertext Markup Language"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is a Hypertext Markup Language"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is a food"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is a food"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is an Asynchronous language"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is an Asynchronous language"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is a styling language"
        },
        {
            "question_text": "What is HTML?",
            "answer_text": "HTML is a styling language"
        }
    ]
    

    这是带有json_encode的所需输出:

    "What is HTML?": {
            "1": "HTML is a Hypertext Markup Language",
            "2": "HTML is a food",
            "3": "HTML is an Asynchronous language",
            "4": "HTML is a styling language"
        }
    

    我目前得到的是多个单个对象,其中有一个答案,但总是与之相关的答案。我希望制作一个包含其中所有答案的单个对象以及表示该对象的问题。我真的希望这是有道理的。我的逻辑可能很偏僻,所以请原谅我。

    我尝试使用while循环,但我无法让它工作。有人能引导我走向实现我想要的输出的正确方法吗?

    谢谢。

4 个答案:

答案 0 :(得分:9)

听起来只是改变你正在建造的阵列......

$encode = array();

while($row = mysqli_fetch_assoc($result)) {
   $encode[$row['question _text']][] = $row['answer_text'];
}

echo json_encode($encode);

答案 1 :(得分:0)

改变这一位:

$encode = array();

while($row = mysqli_fetch_assoc($result)) {
   $encode[] = $row;
}

To(从1开始我添加了$ i,而不是仅仅将其推送到编码数组的末尾):

$encode = array();
$i = 1;
while($row = mysqli_fetch_assoc($result)) {
   $encode[$row['question_text']][$i] = $row['answer_text'];
   $i++;
}

你应该没事。

答案 2 :(得分:0)

您不需要使用PHP来执行额外处理。

只需使用mysql Group By

$stmt = "SELECT questions.question_text, answers.answer_text 
  FROM     questions, answers, test
  WHERE    questions.question_id = answers.question_id
  AND      questions.test_id =1
  GROUP BY questions.question_id;"

答案 3 :(得分:0)

我将查询更改为以下内容:

SELECT DISTINCT questions.question_text, answers.answer_text 
   FROM     questions, answers, test
   WHERE    questions.question_id = answers.question_id
   AND      questions.test_id =

while循环:

while($row = mysqli_fetch_assoc($result)) {
    $encode[$row['question_text']][] = $row['answer_text'];
}

这给了我这个:

{
    "What is HTML?": [
        "HTML is a Hypertext Markup Language",
        "HTML is a food",
        "HTML is an Asynchronous language",
        "HTML is a styling language"
    ]
}

我现在可以使用它。