drzablspw01655860
2013-11-27 22:42
浏览 317
已采纳

用于MySQL输出的Unicode字符编码

I'm trying to retrieve data from a table whose collation is 'utf8_general_ci' and the column titled 'app_name' has string like : Plants vs. Zombies™ 2,포코팡 for Kakao,네이버 - Naver,µTorrent® - Torrent App. When I tried to query the table as below:

   <?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;
 ?>

The jsontable throws null values and for the file 'logfile.txt'the unicode characters is converted to '???' as seen here

图片转代码服务由CSDN问答提供 功能建议

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

 &lt;?php 
 
 $ con = mysql_connect('localhost','root','root')或 die('连接服务器时出错'); 
 
 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 ==“没有IA的应用程序”){
 $ sql =“SELECT app_name FROM  MonthlyAppState WHERE是86 = 0 AND is86 IS NOT NULL和monthYear ='“。$ monthYear。”'“; 
 $ result = mysql_query($ sql); 
} 
 
 
 $ table = array();  
 $ table ['cols'] = array(
array('label'=&gt; $ appType,'type'=&gt;'string')
); 
 
 
 $ rows = array()  ; 
 $ appendingString =“”; 
 while($ r = mysql_fetch_assoc($ result)){
 
 $ temp = array(); 
 $ temp [] = array('v'=&gt;(  string)$ r ['app_name']); 
 $ rows [] = array('c'=&gt; $ temp); 
 $ appendingString = $ appendingString。“/”。$ r ['app_name'];  
} 
 
 $ myFile =“logfile.txt”;  
 $ fh = fopen($ myFile,'w')或死(“无法打开文件”);  
 fwrite($ fh,$ appendingString); 
 fclose($ fh); 
 
 $ table ['rows'] = $ rows; 
 
 //将表格编码为JSON 
 $ jsonTable =  json_encode($ table); 
 
 //设置标题; 前两个阻止IE缓存查询
标题('Cache-Control:no-cache,must-revalidate'); 
 header('Expires:Mon,26 Jul 1997 05:00:00 GMT'); 
标题 ('Content-type:application / json'); 
 
 echo $ jsonTable; 
?&gt; 
   
 
 

jsontable抛出空值, file'logfile.txt'将unicode字符转换为'???' 如此处

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题