php - 使用 JSON php 脚本在 Android 应用程序中的 MySQL 服务器中显示保存在 varchar (utf8_unicode_ci) 中的语言文本

标签 php mysql phpmyadmin

我已经浏览了许多关于同一主题的帖子,但仍然无法找出解决方案。如果您认为可用的解决方案适合我,那么您也可以指导我。

现在,我想在我的应用程序中以我的区域语言(印地语)显示文本。该文本存储在我的网站服务器 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”字段类型。

enter image description here

下面是我的 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 添加到 PostTitlePostBrief 喜欢

enter image description here

关于php - 使用 JSON php 脚本在 Android 应用程序中的 MySQL 服务器中显示保存在 varchar (utf8_unicode_ci) 中的语言文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47939439/

相关文章:

c# - asp.net c#代码中的错误(mysql数据库连接)

mysql - 是否可以从 url 将 sql 文件导入 mysql 数据库

php - mcrypt 和 IIS - 模块不会在 PHP 中加载

mysql - 使用 GROUP BY 获取合适的日期

创建存储过程时MySQL版本差异?

javascript - 从 QR Contact 等网站将联系人数据保存到手机

php - 统计并检查json查询结果

mysql - 是否有任何sql语句来更改mysql中的max_allowed_pa​​cket大小

php - 使用 PHP 确定用户文本数据库中的趋势

php - 亚马逊 MWS 的 Webhooks