php json_encode“格式错误的UTF-8字符,可能编码不正确”

时间:2019-12-28 16:16:39

标签: php mysql json utf-8

我正在真实服务器上创建数据库,但是我有两个大问题

1)json_encode函数返回“格式错误的UTF-8字符,可能编码错误”; 2)当我解决第一个问题时,日语字符返回为'?'问号。

但是,如果我在localhost MAMP服务器中运行相同的php代码,则所有工作均成功;没有错误。

这是PHP文件

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once("../objects/radicale.php");
include_once("../../database/database.php");

$radicaleObj = new Radicale();

$stmt = $radicaleObj->read();
$num = $stmt->num_rows;

if($num > 0){
    $radicaliArray = array();
    while($row = mysqli_fetch_array($stmt)){
        extract($row);

        //JSON_UNESCAPED_UNICODE
        echo(utf8_encode($row[1]));//<-This is the Japanese character that is returned as '?'
        $age=array("Radicale"=>$radicale ,"concettoITA"=>"$concetto_ITA","Joe"=>"43");
//$concetto_ITA is the column row name extracted by extract() function and is reason of the first error
        array_push($radicaliArray, $age);
    }
    echo(mb_convert_encoding($radicale,"UTF-8","auto"));
    //echo json_encode($radicaliArray, JSON_UNESCAPED_UNICODE|JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);
    echo json_encode($radicaliArray);
     // Print out the error if any
    echo json_last_error_msg();
    die(); // halt the script
}
?>

MAMP Localhost服务器和Netson服务器的数据库的字符编码均为utf8_general_ci。

你能帮我吗?我不知道该怎么办才能解决此问题。

如果您需要信息,请询问。

非常感谢您

1 个答案:

答案 0 :(得分:1)

MySQL和PHP需要处理相同的字符集“ UTF-8”,请通过添加以下内容检查是否已实现:

  

mysqli_set_charset($ connection,“ utf8”);

连接到数据库后。另一种方法是,您可以尝试按照以下方式编码为EUC-JP或SHIFT-JIS:

  

mb_convert_encoding(“日本语”,'UTF-8',array('EUC-JP','SHIFT-JIS',   'AUTO'));

相关问题