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条)

报告相同问题?

悬赏问题

  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题