【问题标题】:json_encode() returns null for special charactersjson_encode() 为特殊字符返回 null
【发布时间】:2016-12-08 11:23:41
【问题描述】:

我知道对此有很多问题。我已经尝试了很多东西,但我无法解决它。也许是因为我的知识有限,我未能执行解决方案?

我从 mysql 数据库中选择数据并在其上使用 json_encode() 函数。这有效,除了包含特殊字符(土耳其语)的数据。对于这些值,json 返回 null。我该如何解决这个问题?

这是我的简单 php 代码:

<?php

require('init.php');

$sql="SELECT * FROM tablename;";

$result=mysqli_query($con,$sql);
$response=array();

while($row=mysqli_fetch_array($result)){


array_push($response,array("X"=>$row["x"],"Y"=>$row["y"]));

}

echo json_encode($response);


?>

在 mysql 中,列设置为 utf8-turkish。我尝试过设置标头、调用一些函数、以 utf8 编码重新创建 php 文件等。但没有一个奏效。

【问题讨论】:

标签: php mysql utf-8 turkish


【解决方案1】:

在我的连接脚本中使用 mysqli_set_charset 函数解决了这个问题。在类似的问题上找不到这个。感谢用户“RiggsFolly”。

【讨论】:

    【解决方案2】:

    添加 TRUE: json_decode($string, TRUE); 这将获得 utf8 字符而不是 Unicode 转义序列(如 \u00d6)。

    您需要在连接、表格和 html 中使用 utf8(或 utf8mb4)。见this

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 1970-01-01
      • 2014-01-08
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      相关资源
      最近更新 更多