ñ returns null - jQuery en PHP


ñ returns null - jQuery en PHP

我使用 jQuery 在 PHP 中运行一个函数来从数据库中获取数据。

        $.ajax({
            url: 'service.php',
            data: { functie: type, param: radiobutton },
            dataType: 'json',
            success: function (data) {}
        });

一切正常。除了带有特殊字符的字符串在 succes 函数中返回为 null。例如: ñ

答案

在这里解释:特殊字符中断 json 返回给 jQuery

但它对我不起作用。

在 php 中,我尝试了:

$result = mysql_query("SELECT * FROM wines");
$array = array();
while($row = mysql_fetch_assoc($result)){
    $array[]=htmlentities($row);
}

谁知道如何确保一切都以良好的方式返回?

在 while 语句中,您是在数组上使用htmlentities,而不是字符串(数组中的每个键都是数据库中的一列)。

您可以执行以下任一操作:

使用array_map:

while($row = mysql_fetch_assoc($result)){
    $array[] = array_map("htmlentities", $row);
}

或更改您的 while 循环:

while($row = mysql_fetch_assoc($result)){
    foreach ($row as $key => $value) {
        $row[$key] = htmlentities($value);
    }
    $array[] = $row;
}