【问题标题】:JSON encodes cyryllic as a nullJSON 将西里尔字母编码为空
【发布时间】:2016-09-14 09:10:02
【问题描述】:

我有带有西里尔符号的 MYSQL 表。
This is my MYSQL table

我使用 PHP 获取 MYSQL 结果并将其编码为 JSON。

    <?php

include 'connection.php';

$array_to_json = array();

$query = "SELECT * FROM online";


$result = mysqli_query($link, $query);

mysqli_set_charset("utf8");
while($row = $result->fetch_assoc()) {

$row_array['parameters'] = $row['parameters'];

$row_array['Descriptions'] = $row['Descriptions'];

$row_array['units'] = $row['units'];
      array_push($array_to_json, $row_array);

}

echo json_encode($array_to_json, JSON_UNESCAPED_UNICODE);

$result->close();

?>

结果我得到了空值。 JSON returns null

我做错了什么?

【问题讨论】:

  • 可能是表格结构有问题? prntscr.com/chu681
  • 请使用var_dump($row_array); 检查您的数据是否正在填写。如果不是,则使用mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET utf8"); mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'"); 检查编码问题。最后,请注意 msql_* 已弃用,您应该迁移到 msqli_*

标签: php mysql json cyrillic


【解决方案1】:

你尝试过使用

    $row_array['parameters']    = base64_encode($row['parameters']);
    $row_array['Descriptions']  = base64_encode($row['Descriptions']);
    $row_array['units']         = base64_encode($row['units']);
    array_push($array_to_json, $row_array);

【讨论】:

  • 在使用这些 html 字符之前,您可能仍然需要重新解码它们...@Anton
【解决方案2】:

重新检查你的表结构,确保没有多余的空格

例如:

$row['Descriptions '] 

代替:

$row['Descriptions'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多