【发布时间】:2014-08-14 10:15:23
【问题描述】:
我实际上是第一次在日本工作,但遇到了编码问题。 我正在编写一个小的 PHP 测试脚本,以 JSON 格式解析 SQL 数据库中的一些数据。
SQL 数据库采用 UTF-8 编码,表中包含英文和日文项目。
测试脚本:
header('content-type: text/html; charset=UTF-8');
$link = mysql_connect(DB_NAME, USER_NAME, USER_PASS);
mysql_select_db(SCHEMA_NAME);
$sql = mysql_query("SELECT tab.id, tab.name, tab.indexDescription, gh_region.name AS region\n"
. "FROM (SELECT * FROM gh_building WHERE langId = " . $_GET["lang"] . " AND isPublished = 1) AS tab\n"
. "INNER JOIN gh_region\n"
. "ON tab.regionId = gh_region.id AND tab.langId = gh_region.langId");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print( json_encode($output) );
mysql_close();
当我使用日文参数执行它时,出现编码错误:
[{"id":"1","name":"?????","indexDescription":"??????????????????????JR?????????10?? ","region":"??"},{"id":"3","name":"??????","indexDescription":"??????????????????????????????????????? ??????????????????????? ","region":"??"},{"id":"4","name":"??????","indexDescription":"????????????????????????????????","region":"??"},{"id":"5","name":"?????","indexDescription":"???????????????!?????????????????????????? ","region":"??"},{"id":"6","name":"????","indexDescription":"???????????????????????? ?95????????","region":"??"},{"id":"7","name":"???????","indexDescription":"????????????????? ????????????","region":"??"},{"id":"8","name":"GH??","indexDescription":"????????????????????????????????????????????????????????????","region":"??"},{"id":"2","name":"?????(????) ","indexDescription":"????????????(??????)? ????????????\r\n??????????????","region":"??"},{"id":"9","name":"?????2","indexDescription":"???????????????? ????????????\r\n??????????","region":"??"},{"id":"10","name":"???????????","indexDescription":"","region":"?????"},{"id":"12","name":"??????","indexDescription":"","region":"?????"}]
我尝试了很多事情都没有成功...有什么想法吗?
PS : 我知道我的脚本不好也不安全,这只是一个测试。
谢谢
【问题讨论】:
-
很可能您数据库中的数据编码不同于
UTF-8 -
你能发帖
var_dump($output)吗?
标签: php sql json character-encoding