Quest
我希望获取带有重音字符的行.列(NAME
)的编码是latin1_swedish_ci
.
The Code
The following query returns Abord â Plouffe
using phpMyAdmin:
SELECT C.NAME FROM CITY C
WHERE C.REGION_ID=10 AND C.NAME_LOWERCASE LIKE '%abor%'
ORDER BY C.NAME LIMIT 30
The following displays expected values (function is called db_fetch_all( $result )
):
while( $row = mysql_fetch_assoc( $result ) ) {
foreach( $row as $value ) {
echo $value . " ";
$value = utf8_encode( $value );
echo $value . " ";
}
$r[] = $row;
}
The displayed values: 5482 5482 Abord â Plouffe Abord â Plouffe
然后使用json_encode
对该数组进行编码:
$rows = db_fetch_all( $result );
echo json_encode( $rows );
Problem
Web浏览器接收下列值:
{"ID":"5482","NAME":null}
而不是:
{"ID":"5482","NAME":"Abord â Plouffe"}
(或编码的等效物.)
Question
The documentation states that json_encode()
works on UTF-8. I can see the values being encoded from LATIN1 to UTF-8. After the call to json_encode()
, however, the value becomes null
.
How do I make json_encode()
encode the UTF-8 values properly?
一种可能的解决方案是使用Zend Framework美元,但如果可以避免的话,我宁愿不使用.