这是使用MySQL和UTF8通过UTF8编码获取数据的正确方法

时间:2011-08-16 11:06:50

标签: php mysql objective-c xcode utf-8

<?php
require_once('config.php');
require_once 'convertArraytoJson.php';

$connection=mysql_connect ( "localhost", $databaseuser, $databasepassword );
//mysql_set_charset('utf8'); //Is this better than the last line? 
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'"); //Which one is better?
@mysql_select_db ($databasename) or die ( "Unable to select database" );
$query="SELECT `Building` FROM `tablebuilding` WHERE ID=51";
$data = mysql_query($query);
while (true){
    $info = mysql_fetch_array ( $data, MYSQL_ASSOC );
    if ($info == false) {
        break;
    }
    $output = $info['Building'];
}
//$output is FX Lifestyle X’nter
$v= htmlspecialchars ($output); //Should I use this?


$result = json_encode($output);
//$result is FX Lifestyle X\u2019nter 
echo ($result);

?>

只是想验证这是正确的方法吗?

mysql_set_charset('utf8')mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

之间有什么区别

哪一个更好。

1 个答案:

答案 0 :(得分:1)

JSON只是数据交换的开放标准。如果你发送你的json编码字符串,客户端将转换你的字符串与正确的字符集,如果它支持JSON。 尝试使用客户端JavaScript解释器并检查您的客户端是否支持JSON:

 echo "<script>document.write('$result');</script>";