【问题标题】:Return JSON object from MySQL query using json_encode使用 json_encode 从 MySQL 查询返回 JSON 对象
【发布时间】:2014-02-28 20:04:59
【问题描述】:

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

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

我正在做的事情如下:

  1. 查询与一个问题相关的所有答案
  2. 将其存储到数组中后对其进行编码

这是我当前的查询:

$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);

这给了我这个:

这是 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 的 desired 输出:

"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"
    }

我目前得到的是多个 single 对象,其中包含其中一个答案,但始终是与之关联的答案。我希望制作一个包含所有答案的对象以及代表该对象的问题。我真的希望这是有道理的。我的逻辑可能有点偏离,所以请原谅我。

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

谢谢。

【问题讨论】:

  • 您的问题有唯一的 ID 吗?
  • 您想要的输出既不是来自json_encode 的功能,也不是与mysql 相关的功能。您必须自己重新组织$encode 中的对象。
  • 是的。 @Maximus2012
  • 是否有多个问题需要 json 编码输出,还是只需要 1 个问题?
  • 我认为是嵌套数据库迭代。我认为这将是一件非常合乎逻辑的事情。根据问题 ID,获取与其关联的所有答案。这听起来很对,但我不知道如何编码。

标签: php mysql json


【解决方案1】:

听起来就像只是改变你正在构建的数组......

$encode = array();

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

echo json_encode($encode);

【讨论】:

  • 不完全是 OP 想要的,但这足以提供一个清晰的方法。
  • tbh 问题太长了,我略读了 :)
  • 这给了我一个错误。不输出任何东西。我相信它应该。我什至从“question _text”中删除了空格,但没有运气。
  • @TiffanyLowe 您收到的错误信息是什么?
  • 这个问题确实帮助了我做我想做的事,我的身边稍作调整。
【解决方案2】:

改变这一点:

$encode = array();

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

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

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

你应该没事的。

【讨论】:

    【解决方案3】:

    您不需要使用 PHP 进行额外的处理。

    只用mysqlGroup 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;"
    

    【讨论】:

    • 这只会返回一个问题。
    • 能否简单介绍一下您的表结构
    【解决方案4】:

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

    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"
        ]
    }
    

    我现在可以使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2011-05-29
      • 2012-09-28
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      相关资源
      最近更新 更多