【问题标题】:JSON encode error for bengali fonts孟加拉字体的 JSON 编码错误
【发布时间】:2016-08-01 07:39:26
【问题描述】:

一切正常。但是当我尝试从 mysql 数据库中编码一些孟加拉语字体时,问题发生了。孟加拉语字体显示为“?????????????”。

** php文件**

<?php 
define('HOST','localhost');
define('USER','xxxxxxx');
define('PASS','xxxxxxxx');
define('DB','xxxxxxxxx');

$con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from bookinfo ORDER BY ID DESC";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
    'name'=>$row[1],
    'writter'=>$row[2],
    'url'=>$row[3]
     ));
}

echo json_encode(array("result"=>$result), JSON_UNESCAPED_UNICODE);

mysqli_close($con);

?>

click here to see output result

【问题讨论】:

标签: php mysql json


【解决方案1】:

首先设置内容类型,例如。 header('Content-Type: application/json; charset=utf-8')

然后在执行查询之前设置 utf-8 代码

像这样:

<?php
header('Content-Type: application/json; charset=utf-8');    
define('HOST','localhost');
define('USER','xxxxxxx');
define('PASS','xxxxxxxx');
define('DB','xxxxxxxxx');    
$con = mysqli_connect(HOST,USER,PASS,DB);    
mysqli_query($con,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

$sql = "select * from bookinfo ORDER BY ID DESC";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_row($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'writter'=>$row[2],
'url'=>$row[3]
));
}   
echo json_encode(array("result"=>$result), JSON_UNESCAPED_UNICODE);    
mysqli_close($con);    
?>

它会按照您的预期为您提供正确的字体。

【讨论】:

    【解决方案2】:
     header('Content-Type: application/json; charset=utf-8');
    

    【讨论】:

    • 您的表格字段应该使用 UTF-8 作为编码
    • 其实我想把它解析成安卓应用。所以这里不需要桌子。
    • 如果您的数据库中有孟加拉语文本,那么您不必担心!要求android dev在设备端对其进行编码!!图像中的结果供您理解,它只是您的临时输出!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 2018-02-20
    • 2017-09-06
    相关资源
    最近更新 更多