简体   繁体   English

PHP json 格式从mysql 中检索表示Unicode 中的数据

[英]PHP json format retrieve from mysql represents data in Unicode

I have some persian data in my mysql database, the retrieved data contains C/C++/Java Source Code encoded data for persian values as below;我的 mysql 数据库中有一些波斯数据,检索到的数据包含波斯值的 C/C++/Java 源代码编码数据,如下所示;

{"server_response":[{"hp_number":"09301234567","name":"\س"}]} while in name column there is a persian character (س). {"server_response":[{"hp_number":"09301234567","name":"\س"}]} 而在 name 列中有一个波斯字符 (س)。

the php code is as below: php代码如下:

<?php

require "init.php";

$hp_no = "09301234567";
mysqli_set_charset($con,"utf8");

$sql = "select * From users Where hp_number = ('$hp_no');";
$result = mysqli_query($con,$sql);

$response = array();

while ($row = mysqli_fetch_array($result)) {
  array_push($response,array("hp_number"=>$row[1],"name"=>$row[3]));
}

echo json_encode(array("server_response"=>$response));

mysqli_close($con);
?>

on mysql side encoding is set as utf8_general_ci for both table and database itself and is presented perfectly with no issue.在 mysql 端,表和数据库本身的编码都设置为 utf8_general_ci,并且完美呈现没有问题。

Thanks in advance for your helps and suggestions.在此先感谢您的帮助和建议。

I could successfully solve the issue by adding JSON_UNESCAPED_UNICODE as another parameter while encoding.我可以通过在编码时添加 JSON_UNESCAPED_UNICODE 作为另一个参数来成功解决这个问题。 so the code line changes to;所以代码行更改为;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM