【问题标题】:Show json from database显示来自数据库的 json
【发布时间】:2021-11-28 09:33:54
【问题描述】:

我正在使用显示测验的 json 结构,但现在我不明白如何正确显示它,请帮助我,我想从数据库中读取信息并显示在下面的 json 结构中,谢谢

[
  {
    "question_id" : 0,
    "question": "What is the original native name of Mount Rainier?",
    "answers": [
      {"id": 0, "text": "Tahoma"},
      {"id": 1, "text": "Shuksan"},
      {"id": 2, "text": "Stillaguamish"}

    ],
    "correct": 0
  },
  {
    "question_id" : 1,
        "question" : "Which image shows a close-up of the Nisqually Glacier?",
        "answers"  : [
            {"id"  : 0, "image" : "images/quizImages/glacier_01.jpg" },
            {"id"  : 1, "image" : "images/quizImages/glacier_02.jpg" },
            {"id"  : 2, "image" : "images/quizImages/glacier_03.jpg" },
            {"id"  : 3, "image" : "images/quizImages/glacier_04.jpg" }
        ],
        "correct"  : 3
    }
]

【问题讨论】:

  • 你使用 MySQLi 还是 PDO ?
  • 你到底卡在哪里了?请参阅tour 和How to Ask...您需要提出一些更专注和具体的问题。我们不知道您是否甚至知道如何编写 SELECT 查询,或者是否存在关于 JSON 或其他什么的更特殊的问题。
  • @The House 请分享您的努力,直到您尝试过的地方

标签: php mysql json


【解决方案1】:
  1. 从数据库中获取您的数据并使用正在获取数据库的数据创建multidimensional array。

  2. 在最终的array 上应用json_encode

【讨论】:

    猜你喜欢
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多