php - PHP json_encode 和 utf8_encode 不适用于我的 php 文件
问题描述
我有一些需要编码的 json - 越南语单词。我尝试使用 JSON_UNESCAPED_UNICODE 但看起来它不再工作了。
header("Content-type: application/json; charset=utf-8");
$db = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysqli_connect_errno());
$result = mysqli_query($db,"SELECT * FROM categories") ;
if (mysqli_num_rows($result) > 0) {
$response["categories"] = array();
while ($row = mysqli_fetch_array($result)) {
$categories["id"] = json_encode($row["id"]);
$categories["name"]= json_encode($row["name"],JSON_UNESCAPED_UNICODE);
array_push($response["categories"], $categories);
}
$response["result"] = "OK";
$myJSON = json_encode($response);
echo $myJSON;
}
输入:
id=1 name=Truyện Cổ Tích
输出:
id = 1 名称 = 假
这是我第一次尝试 PHP。我的 php 版本是 7.2.8。
解决方案
- 第一次运行
mysqli_query('SET NAMES utf8')
。 - 打印错误信息
json_last_error()
推荐阅读
- javascript - 打字稿中构造函数中新对象的类型是什么?
- python-3.x - 找不到满足 MetaTrader5 要求的版本
- laravel - .Illuminate\Broadcasting\BroadcastException 在 laravel 6
- javascript - 如何在图像的背面显示信息?
- vb.net - 无法从其他表单更改用户控件中组合框的值
- javascript - jQuery $.each - 遍历对象数组并为每个对象选择一个字段
- sas - 2 个 proc 报告之间的 SAS 空白
- typescript - 为什么在打字稿中更改 vsc 突出显示颜色 [无法使用选项“增量”指定选项“noEmit”]
- python - 简单 2D 物理引擎的摩擦和碰撞问题
- python - 定义networkx中的节点级别