我创建了一个数据库,它的字符集是 utf8mb4_general_ci。 现在我正在尝试为这个数据库构建 api,但是我在显示阿拉伯数据时遇到了问题,这是我的配置 php 文件:
<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "project";
$conn = new mysqli($server,$user,$pass,$dbname);
if($conn->connect_error) {
echo "<h1> Failed to connect</h1>";
}
?>
这是我的 api php 文件:
<?php
header("Content-Type: application/json");
require_once 'config.php';
$charset = "UTF-8";
$conn->set_charset($charset);
$query = "select * from users";
$result = $conn->query($query);
$output = [];
while($item = $result->fetch_assoc()) {
$output[] = $item;
}
echo json_encode($output,JSON_PRETTY_PRINT);
?>
这是我的结果json文件
[
{
"ID": "1",
"username": "\u0645\u062d\u0645\u062f",
"password": "user1"
},
{
"ID": "2",
"username": "\u062d\u0645\u062f",
"password": "user2"
}
]
如何显示带有阿拉伯字符的用户名?