【问题标题】:Json from Mysql with PHP来自 MySQL 的 Json 和 PHP
【发布时间】:2014-09-03 14:49:59
【问题描述】:

嗨,我正在用 php 抓取我的桌子并得到这个输出

{"id":"1","player":"123","name":"his_name","reason":"reason text","instance":"1","lastupdated":"datetime"}

但是我希望得到这样的东西

text{"id":"1","player":"123","name":"his_name","reason":"reason text","instance":"1","lastupdated":"datetime"}

当前 php:

mysql_connect($dbhost.':'.$dbport, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db("dbname") or die (mysql_error());

$sth = mysql_query("SELECT *"."FROM `table_name` Order By id Desc;");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
}

print json_encode($rows);

数据库结构:

player | name | reason | instance | lastupdated

任何帮助将不胜感激

【问题讨论】:

  • 为什么要附加text
  • 我轮询它的方式我需要在那里有文本,我已经在各种 API 上看到它,例如 twitch 的 kraken api。
  • 添加这样的文本会使 json 无效。除非您将其作为 jsonp 类型响应之王,否则在 json 字符串之外添加文本是没有意义的。

标签: php mysql json


【解决方案1】:

如果你想在前面加上文字,你可以使用字符串连接操作符来打印:

print "text" . json_encode($rows);

这对你有用吗?但是text 的内容应该是什么?

【讨论】:

【解决方案2】:

如果您只想在从数据库获得的完整结果集之前添加一个键。正如我从您的示例链接中了解到的那样。

然后你可以像这样更新你的代码:

 // Assuming $rows is already populated with desired data.

$res = array();
$res['text'] = $rows;

print_r(json_encode($res));

输出:

{"text":{"id":1,"player":123,"name":"his_name","reason":"reason text","instance":1,"lastupdated":"datetime"}}

希望对你有帮助。

【讨论】:

  • 正是我在寻找它只是我不知道如何解释它。
猜你喜欢
  • 2018-08-19
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
相关资源
最近更新 更多