我有一个php文件来返回json,但是结果不正确。
例:=> L?p ?Trμnh ?ng ?ng数据库中的Lập TrμnhỨng ng D?ng
这里是php文件
<?php
$mysql = new mysqli('localhost','root','','freelancer');
$result = $mysql->query("select * from tag");
$rows = array();
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$rows[] = array_map("utf8_encode", $row);
}
echo json_encode($rows);
?>希望大家都能帮助我阅读this.Thanks
发布于 2016-01-30 15:38:52
utf8_encode()只适用于编码为iso-8895-1的字符串.由于您的字符串包含非拉丁字符,所以您的数据不能用iso-8895-1编码,所以utf8_encode()肯定无法工作。
相反,您需要要求MySQL通过将set_charset("utf8")添加到您的连接配置中,返回以UTF-8编码的数据。现在您有了UTF-8编码字符串,您不再需要重新编码它.
新代码应该如下所示:
<?php
$mysql = new mysqli('localhost','root','','freelancer');
$mysql->set_charset("utf8")
$result = $mysql->query("select * from tag");
$rows = array();
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$rows[] = $row;
}
echo json_encode($rows);
?>https://stackoverflow.com/questions/35102352
复制相似问题