今天测试了一下从MySQL中获得中文数据,发现需要进行一些处理才能在网页中完美显示中文(在PHPMYADMIN中插入、显示是正常的):
首先,当然是你的MySQL数据库是基于UTF-8建立的。 然后是在PHP中的常规连接:
$hostname_test = localhost;
$database_test = test;
$username_test = root;
$password_test = xxxxxx;
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(),E_USER_ERROR);
$query=select * from chinese; mysql_select_db($database_test, $test);
**$xx=set names utf8; mysql_query($xx);**
$rs=mysql_query($query, $test);
while($row=mysql_fetch_object($rs))
{
echo $row->id.<br/>;
echo $row->desc.<br/>;
echo =====================
<br/>;
}
注意上面的黑体部分,这两行很关键。
再然后是在PHP文件中要指定字符集:
<head> <meta http-equiv=Content-Type content=text/html; charset=utf-8> </head>
注意,这里要写utf-8,而set names时要写utf8。
Leave a Reply to 生活在远方 » Blog Archive » 八月十七日 Cancel reply