【问题标题】:Empty json string when using json_encode function使用 json_encode 函数时为空 json 字符串
【发布时间】:2015-12-13 22:54:50
【问题描述】:

我在 php 中使用 json_encode() 函数遇到了一个非常奇怪的问题。我有 2 个包含数据的表。在表 1 中只有 2 行。使用下面的代码可以很好地与 json_encode() 函数配合使用。在表 2 中,我有 50,000 行可供我选择(例如 1000)行。使用相同的代码不会导致我使用 json 字符串。

我使用的代码是这样的:

$array = array();
while($row = mysqli_fetch_assoc($result)) {
    $array[] = $row;
}
echo count($array);
echo json_encode($array);

如何让 json_encode() 也适用于第二张表(更大的一张)?

【问题讨论】:

  • 它会抛出任何错误吗?也许您正在达到最大执行时间?
  • 没有抛出错误。使用 strlen() 函数检查字符串的长度返回 0(因此字符串为空)。
  • 检查你的memory limit...
  • ini_get('memory_limit') 返回 memory_limit = 75M。如果从 50k 行的表中进行选择查询,例如 949 行,这可能是个问题吗?
  • 错误报告开启了吗?你在strlen(json_encode($array)) 上得到0count($array) 是否返回值? $array 长什么样子?

标签: php mysql json


【解决方案1】:

我找到了问题的答案:我必须将整个编码结构更改为 UTF-8。所以我改变了:

  • 数据库编码为 UTF-8(在 PhpMyAdmin 中)
  • 表编码为 UTF-8(在 PhpMyAdmin 中)
  • $connection->set_charset("utf8");的连接编码
  • 填充数据库的java程序中的url编码(获取和发布数据)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2013-10-22
    • 2014-01-08
    相关资源
    最近更新 更多