2012-08-14 61 views
0

我使用jQuery在PHP中运行函数以从数据库获取数据。ñ返回null - jQuery en PHP

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

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

这个答案就在这里解释: Special characters break json returned to jQuery

但它不为我工作。

在PHP我想:

$result = mysql_query("SELECT * FROM wines"); 
$array = array(); 

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

谁知道如何确保一切回报的好方法?

+0

阅读:[处理Unicode的前向后在一个Web应用程序](http://kunststube.net/frontback/) – deceze 2012-08-14 12:15:48

回答

1

在你的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; 
}