Php 用于MySQL输出的Unicode字符编码
我试图从一个排序规则为“utf8\u general\u ci”的表中检索数据,标题为“app\u name”的列具有类似字符串:Plants vs.Zombies™ 2.포코팡 对于卡考来说,네이버 - Naver,µTorrent®-Torrent应用程序。当我尝试按如下方式查询表时:Php 用于MySQL输出的Unicode字符编码,php,mysql,unicode,Php,Mysql,Unicode,我试图从一个排序规则为“utf8\u general\u ci”的表中检索数据,标题为“app\u name”的列具有类似字符串:Plants vs.Zombies™ 2.포코팡 对于卡考来说,네이버 - Naver,µTorrent®-Torrent应用程序。当我尝试按如下方式查询表时: <?php $con = mysql_connect('localhost', 'root', 'root') or die('Error connecting to server');
<?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;
?>
使用
在mysql\u connect
之后设置客户端字符集
参考资料:
你主张使用一个不推荐使用的函数?@Phil:假设它可能有10Mb的代码-我不认为重写所有函数比添加一行要好多少。特别是如果OP不打算在不久的将来升级他们的php,他也会得到一个SET NAMES utf8
querymysqli_SET_charset($con,'utf8')
是一个不推荐的版本,您应该真正避免使用mysql,而是使用mysqli或pdo之类的工具。它使保护自己免受sql注入变得微不足道,而常规mysql类清理查询的负担总是落在您身上,而不是落在您的技术上。您从中选择的列的字符集(而不是排序规则)是什么?它的utf8表示应用程序名称
mysql_set_charset('utf8', $con);