从MySQL中获取中文数据

今天测试了一下从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。

About tr

I am a good person and I am sure you are talking to a good person.
This entry was posted in 编程、软件、技术 and tagged , . Bookmark the permalink.

One Response to 从MySQL中获取中文数据

  1. Pingback: 生活在远方 » Blog Archive » 八月十七日

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">