我已经浏览了许多关于同一主题的帖子,但仍然无法找出解决方案。如果您认为可用的解决方案适合我,那么您也可以指导我。
现在,我想在我的应用程序中以我的区域语言(印地语)显示文本。该文本存储在我的网站服务器 MySQL 数据库的表中。我正在通过以 JSON 形式运行的 php 脚本运行 SQL 查询来检索这些数据以及更多列。
当我在 JSONLint 上测试此 JSON 并通过记录应用程序内异步任务接收到的 json 字符串时,所有具有 Varchar 类型和“utf8_unicode_ci”排序规则的字段都显示如下,并带有问号。其他字段类型正确。
"?????? ??? ???? ?? ??????? ?? ????? ???? ?? 10 ?????"
服务器详细信息
phpMyAdmin Version 4.7.3
PHP version: 5.6.30
Database
Server type: MySQL
Server version: 5.6.38 - MySQL Community Server (GPL)
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)
下面是我在数据库中的表。图像中 utf8 的差异是因为我正在尝试 utf8mb4 是否有效。 数据库是MyISAM类型,因此没有“nvarchar”字段类型。
下面是我的 php 脚本。我有两个表,表 1 包含非本地化字段,表 2 包含本地化翻译字段。 “hi”是我的语言代码。上面的表格图片是TableTranslation的。
<?php
$host='localhost';
$hostname='*******';
$password='*******';
$db='*******';
$sql="SELECT TablePost.PostID,TableTranslation.PostCategory,TablePost.PostImage,TableTranslation.PostTitle,TableTranslation.PostBrief,
TableTranslation.PostLink,TablePost.IsShort,TablePost.IsSeenBy
FROM TablePost LEFT JOIN TableTranslation ON TablePost.PostID = TableTranslation.PostID
WHERE TableTranslation.Language LIKE 'hi';";
$con=mysqli_connect($host,$hostname,$password,$db);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result)){
array_push($response, array(
"PostID" => $row[0],
"PostCategory" => $row[1],
"PostImage" => $row[2],
"PostTitle" => $row[3],
"PostBrief" => $row[4],
"PostLink" => $row[5],
"IsShort" => $row[6],
"IsSeenBy" => $row[7]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($con);
?>
下面是我收到的 JSON 响应。
{
"server_response": [{
"PostID": "1",
"PostCategory": "??????????",
"PostImage": "http:\/\/websitename.com\/image.png",
"PostTitle": "?????? ??? ???? ?? ??????? ?? ????? ???? ?? 10 ?????",
"PostBrief": "???? ?? ????????? ?? ???-??? ?????? ?? ??? ???????? ???? ???? ?? ?? ??? ????? ??? ??? ?????? ????? ???? ???? ??? ?? ?? ???? ?? ??????? ?? ????? ???? ?? ??? ???????? ????? ???? ????",
"PostLink": "http:\/\/websitename.com\/post_link\/",
"IsShort": "0",
"IsSeenBy": "98"
}]
}
我该如何纠正它?非常感谢这里的任何帮助。
最佳答案
在 mysqli_connect()
之后添加行 mysqli_set_charset( $con, 'utf8');
就像下面的代码
<?php
$host='localhost';
$hostname='*******';
$password='*******';
$db='*******';
$sql="SELECT TablePost.PostID,TableTranslation.PostCategory,TablePost.PostImage,TableTranslation.PostTitle,TableTranslation.PostBrief,
TableTranslation.PostLink,TablePost.IsShort,TablePost.IsSeenBy
FROM TablePost LEFT JOIN TableTranslation ON TablePost.PostID = TableTranslation.PostID
WHERE TableTranslation.Language LIKE 'hi';";
$con=mysqli_connect($host,$hostname,$password,$db);
mysqli_set_charset( $con, 'utf8'); //change here.
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result)){
array_push($response, array(
"PostID" => $row[0],
"PostCategory" => $row[1],
"PostImage" => $row[2],
"PostTitle" => $row[3],
"PostBrief" => $row[4],
"PostLink" => $row[5],
"IsShort" => $row[6],
"IsSeenBy" => $row[7]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($con);
?>
还尝试将排序规则 utf8_general_ci
添加到 PostTitle
和 PostBrief
喜欢
关于php - 使用 JSON php 脚本在 Android 应用程序中的 MySQL 服务器中显示保存在 varchar (utf8_unicode_ci) 中的语言文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47939439/