用于MySQL输出的Unicode字符编码

时间:2013-11-27 22:42:49

标签: php mysql unicode

我正在尝试从排序为'utf8_general_ci'的表中检索数据,标题为'app_name'的列的字符串如下:Plants vs. Zombies™2,포코팡for Kakao,네이버 - Naver,μTorrent® - Torrent应用。当我尝试查询表格时如下:

   <?php

   $con = mysql_connect('localhost', 'root', 'root') or die('Error connecting to server');

   mysql_select_db('googleappanalysis', $con); 

  if(isset($_POST['appType']))
  {
    $appType = $_POST['appType'];
    $month = $_POST['month'];
    $year = $_POST['year'];
  }


  $monthYear = $month.$year; 

  mysql_query("SET NAMES utf8")

  if($appType == "Apps with IA" ){
    $sql="SELECT app_name  FROM MonthlyAppState WHERE is86 = 1 AND is86 IS NOT NULL AND monthYear = '".$monthYear."'";

    $result=mysql_query($sql);
   }elseif($appType == "Apps with no IA" ){
    $sql="SELECT app_name  FROM MonthlyAppState WHERE is86 = 0 AND is86 IS NOT NULL AND monthYear = '".$monthYear."'";
    $result=mysql_query($sql);
   }


    $table = array();
    $table['cols'] = array(
array('label' => $appType, 'type' => 'string')
 );


 $rows = array();
 $appendingString = "";
 while($r = mysql_fetch_assoc($result)){

    $temp = array();
$temp[] = array('v' => (string)$r['app_name']);
$rows[] = array('c' => $temp);
    $appendingString = $appendingString."/".$r['app_name'];
 }

 $myFile = "logfile.txt";  
 $fh = fopen($myFile, 'w') or die("can't open file"); 
 fwrite($fh, $appendingString);
 fclose($fh);

 $table['rows'] = $rows;

 // encode the table as JSON
 $jsonTable = json_encode($table);

  // set up header; first two prevent IE from caching queries
 header('Cache-Control: no-cache, must-revalidate');
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
 header('Content-type: application/json');

 echo $jsonTable;
 ?>

jsontable抛出空值,对于文件'logfile.txt',unicode字符被转换为'???'见here

1 个答案:

答案 0 :(得分:3)

使用

mysql_set_charset('utf8', $con);

mysql_connect之后设置您的客户端字符集。

参考文献: