dsbm49845 2016-10-19 13:55
浏览 207
已采纳

在MySQL中获取行并返回JSON

I have the following SQL statement. "SELECT * FROM table"

Than I do: echo json_encode(mysqli_fetch_all($query));

Which gives me a json like this:

[
  [
    "32",
    "John",
    "12346",
    "email@gmail.com",
  ]
]

This is all fine, but it's missing the field names.

How could I get a JSON like this:

[
  [
    "id" : "32",
    "name" : "John",
    "phone" : "12346",
    "email" : "email@gmail.com",
  ]
]

Thanks!

  • 写回答

2条回答 默认 最新

  • douchui1488 2016-10-19 13:58
    关注

    it seems you get only numeric indizes. try fetching your data as associative array:

    echo json_encode(mysqli_fetch_all($query, MYSQLI_ASSOC));
    

    see also the manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?