【问题标题】:PHP build a specific JSON string from postgresql DatabasePHP 从 postgresql 数据库构建一个特定的 JSON 字符串
【发布时间】:2012-11-01 16:01:50
【问题描述】:

我想在我的 postressql 数据库中使用 zachhunters json to html tabel 脚本,所以我需要一个特定的 JSON 字符串格式,但我不知道如何构建它......(我是 json 新手)

Link to the Script

我使用

获取我的数据
$result = pg_fetch_array($rs, NULL, PGSQL_ASSOC);

当我使用json_encode($result) 时,我明白了:

{"id":"2","surname":"Max","name":"Muster"} etc.

对于脚本,我需要类似的东西:

{ "d": "[{\"id\":1,\"Username\":\"Sam Smith\"},{\"id\":2,\"UserName\":\"Fred Frankly\"}]" }

我该如何处理?

【问题讨论】:

  • 我非常怀疑您是否需要包含 JSON 字符串的 JSON。您可能需要包含包含行对象的数组的 JSON。

标签: php string postgresql json


【解决方案1】:

在从数据库中获取元素的每个循环中,将 $result 存储在数组 $rows 中。

在循环之后,只需使用 json_encode(array('d' => $rows)) 创建包含所有数据的 JSON。

【讨论】:

    猜你喜欢
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多