-1

In database, I have set a column in utf8_general_ci and use to put some chinese words. And I can see the words in this way "香港", but when I select the data by php and echo the words they become this "\u9999\u6e2f". So how can I get a real chinese words like "香港".

<?php
include 'dbconfig.php'; //search queue by uid, display json

$link = mysql_connect($host, $uname, $pass) or
die("Database connection failed!");

mysql_select_db($database, $link) or
  die("Database could not be selected!");

  $keyword = $_POST['keyword'];

$query = "SELECT qid, qname, description FROM queuing WHERE setUpUid = '".$keyword."' AND isValid = true";
$result = mysql_query($query) or die("Fail");;

$cart = array();
while($array = mysql_fetch_array($result)){
  $cart[] = array(
  "qid" => $array['qid'],
  "qname" => $array['qname'],
  "description" => $array['description']
  );

}
echo json_encode(array("response"=>$cart));

mysql_close($link);
?>

ouput: {"response":[{"qid":"6","qname":"\u9999\u6e2f","description":"\u9999\u6e2f"}]}

We Rub Chan
  • 205
  • 1
  • 5
  • 14
  • http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Mihai Jan 25 '14 at 17:42
  • possible duplicate of [PDO query returns lots of \uXXXX character codes which I can't convert to unicode characters](http://stackoverflow.com/questions/16583954/pdo-query-returns-lots-of-uxxxx-character-codes-which-i-cant-convert-to-unicod) – deceze Jan 25 '14 at 18:20

1 Answers1

0

If you are using PHP 5.4.0 or newer, you have to set JSON_UNESCAPED_UNICODE:

echo json_encode(array("response"=>$cart), JSON_UNESCAPED_UNICODE);

to prevent unicode literals from being escaped. Reference here:

JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80