0

My following code works fine.

$arr = array();
$str = "Acme® Foundation";
$arr[] = array( "title" => $str);
echo json_encode($arr);

--------- Output is as expected

[{"title":"Acme\u00ae Foundation"}]

but if I retrieve same string from database table then it returns null

$arr = array();
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$str = $row["title"];
$arr[] = array( "title" => $str);
echo json_encode($arr);

--------- Output is not as expected

[{"title":null}]

Can someone please guide as I am very new too php and mysql. If I don't use ® symbol then it works just fine.

TIA,

4

2 回答 2

0

该手册指出 json_encode 仅适用于 UTF-8 编码数据。会不会是从数据库中检索到的值有另一种编码?

试试看echo json_encode(utf8_encode($arr));这是否有什么不同。

于 2013-06-22T14:46:26.417 回答
0
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $r[] = array(
      "id" => $row['id'],
      "data1" => $row['data1']
    );
   )
$encoded = json_encode($r);
于 2013-06-22T14:29:34.717 回答