问题描述
我正在使用PHP从MySQL数据库中获取一些具有UTF8归类( utf8_unicode_ci
)的数据.我正在使用这段代码:
I am fetching some data having UTF8 collation (utf8_unicode_ci
) from a MySQL database with PHP. I am using this piece of code:
function advancedDatabaseSearch($pattern, $lpref) {
$link = mysql_connect(DB_URL, DB_USER, DB_PWD);
if (!$link) {
return 'Could not connect: ' . mysql_error();
}
$esc_value = mysql_real_escape_string($pattern);
$esc_lpref = mysql_real_escape_string($lpref);
mysql_select_db(DB_NAME, $link);
$query = "SELECT RAWVALUE FROM rawvalueitem "
."WHERE RAWVALUE LIKE '".$esc_value."' "
."AND LANGUAGE = '".$esc_lpref."' "
."ORDER BY RAWVALUE ASC";
$result = mysql_query($query);
$return = "";
while($row = mysql_fetch_array($result)) {
$return = $return.$row['RAWVALUE']." ";
}
mysql_close($link);
return $return;
}
,然后从Ajax调用的php中获取:
and then from the php called by Ajax:
$result = advancedDatabaseSearch($tttmp, $lpref);
echo $result;
return;
但是,当我在文本区域中显示结果时,重音无法正确显示:
Yet, when I display the result in a text area, the accents are not displayed properly:
另一方面,当我从文件中获取UT8数据时:
On the other side, when I fetch UT8 data from a file:
if ( $file_loc != NULL ) {
if ( file_exists($file_loc) ) {
$handle = fopen($file_loc, "rb");
$contents = fread($handle, filesize($file_loc));
fclose($handle);
$result = $contents;
}
}
echo $result;
return;
我没有收到这个问题!使用PHP从MySql获取数据时如何解决?
I don't get this issue !!! How can I solve it when using PHP to fetch data from MySql?
推荐答案
您是否已将UTF-8设置为数据库连接的默认字符集?
Did you set UTF-8 as the default character set of your database connection?
mysql_set_charset('utf8',$ link);
http://www.php.net/manual/zh/function.mysql-set-charset.php
此外,您的页面上是否包含带有正确字符集的< meta>
标记?
Also, does your page have a <meta>
tag with the correct character set?
这篇关于取PHP时无法正确显示带有重音符号的MySql UTF8归类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!