PHP脚本在ö;上崩溃/é;


PHP script crashes on ö/é

我有一个mysql数据库,使用php脚本从数据库中获取所有行和值,并将它们输出到json文件中。只要里面没有特殊的字符,它就可以很好地工作。"''和/或"等完美地工作,只有ö、äorû等打破了它,什么也没发生。怎么了?

<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("SELECT * FROM my_table") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
    $response["arr"] = array();
    while ($row = mysql_fetch_array($result)) {
        $arr = array();
        $arr["id"] = $row["id"];
        $arr["version"] = $row["version"];
        $arr["info"] = $row["info"];
        // push single info into final response array
        array_push($response["arr"], $arr);
    }
    // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "Nothing found";
    echo json_encode($response);
}
?>

找到解决方案:

Add this to the PHP file:
if (!mysql_set_charset('utf8', $conn)) {
    echo "Error: Unable to set the character set.'n";
    exit;
}

您可以尝试使用JSON_UNESCAPED_UNICODE,如下所示:

echo json_encode($reponse, JSON_UNESCAPED_UNICODE)

参考编号:http://se2.php.net/manual/fr/json.constants.php