【发布时间】:2013-05-11 03:22:53
【问题描述】:
我有一个 MySQL 数据库表,其中存储了不同语言的国家/地区名称,但我无法以 unicode 字符显示数据 - 我只能在特殊字符应该出现的位置显示 \uXXXX 代码。
查询用于 AJAX 请求,结果编码为 JSON 对象。
这是表格(截断):
CREATE TABLE IF NOT EXISTS `tbl_countries` (
`ccode` varchar(2) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`country_en` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`country_fr` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
`country_de` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
`country_es` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
`country_ru` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
`country_tr` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
`country_ar` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ccode`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `tbl_countries`
--
INSERT INTO `tbl_countries` (`ccode`, `country_en`, `country_fr`, `country_de`, `country_es`, `country_ru`, `country_tr`, `country_ar`) VALUES
('AF', 'Afghanistan', 'Afghanistan', 'Afghanistan', 'Afganistán', 'Афганистан', 'Afganistan', 'أفغانستان'),
('AX', 'Aland Islands', 'Îles Åland', 'Alandinseln', 'Islas Åland', 'Аландские острова', 'Aland Adaları', 'جزر أولان'),
('AL', 'Albania', 'Albanie', 'Albanien', 'Albania', 'Албания', 'Arnavutluk', 'ألبانيا'),
('DZ', 'Algeria', 'Algérie', 'Algerien', 'Argelia', 'Алжир', 'Cezayir', 'الجزائر'),
('AS', 'American Samoa', 'Samoa américaines', 'Amerikanisch-Samoa', 'Samoa Americana', 'Американское Самоа', 'Amerikan Samoası', 'ساموا الأمريكية');
这是创建 PDO 的代码:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",
$dbuser,
$dbpass,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$return_arr = array ();
if ($conn) {
$ac_term = $_GET['term'];
$query = "SELECT * FROM `tbl_countries` WHERE `country_en` LIKE :term";
$result = $conn->prepare ($query);
$result->bindValue (":term", "%".$ac_term."%");
$result->execute ();
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch (PDO::FETCH_ASSOC)) {
$row_array['country_en'] = $row['country_en'];
$row_array['country_de'] = $row['country_de'];
$row_array['country_es'] = $row['country_es'];
$row_array['country_fr'] = $row['country_fr'];
$row_array['country_ru'] = $row['country_ru'];
$row_array['country_tr'] = $row['country_tr'];
$row_array['country_ar'] = $row['country_ar'];
$row_array['ccode'] = $row['ccode'];
array_push ($return_arr, $row_array);
}
}
unset ($conn);
echo json_encode ($return_arr);
PHP 脚本的开头是以下行:
header('Content-Type: text/html; charset=utf-8');
这是我输入搜索词united%20king时得到的典型输出:
[{
"country_en":"United Kingdom",
"country_de":"Vereinigtes K\u00f6nigreich",
"country_es":"Reino Unido",
"country_fr":"Royaume-Uni",
"country_ru":"\u0412\u0435\u043b\u0438\u043a\u043e\u0431\u0440\u0438\u0442\u0430\u043d\u0438\u044f",
"country_tr":"Birle\u015fik Krall\u0131k",
"country_ar":"\u0627\u0644\u0645\u0645\u0644\u0643\u0629 \u0627\u0644\u0645\u062a\u062d\u062f\u0629",
"ccode":"GB"
}]
在 PHP 代码中,我尝试使用 htmlentities,但它仅显示了德语输出的特殊字符:
$row_array['country_de'] = htmlentities ($row['country_de'], ENT_QUOTES, "UTF-8");
我错过了什么?感谢阅读。
【问题讨论】:
-
听起来您正在阅读 latin1 页面上的 UTF8 数据。仔细检查您的 HTML 或 json 页面发送的标题。
-
见stackoverflow.com/questions/2934563/…。也尝试运行
SET CHARSET 'utf8',以防您的默认编码不是UTF8。 -
我在PHP文件开头使用
header('Content-Type: text/html; charset=utf-8');来设置字符集。 -
感谢您的链接和建议
标签: php json unicode utf-8 pdo