我使用 WAMP 服务器。 (Windows、Apache、MySQL、PHP)。
//很重要
FIRST :
在 phpmyadmin 或 MySQL 中:
确保Mysql数据库是utf。
确保您的数据库及其表是 utf-general-ci
立即连接到 Mysql 后(在选择数据库之前)执行此命令。
mysql_set_charset('utf8');
例子 :
<?php
//connect to MySQL
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_set_charset('utf8'); // that's the order.
echo "Connected to MySQL<br />";
//connect to your DB
mysql_select_db("mydb") or die(mysql_error());
echo "Connected to Database";
?>
第二 :
在 php 文件的元数据中,元数据如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
第三 :
确保 php 文件本身已启用 utf-8 。你可以在你的 IDE 设置中确认,或者如果你使用 notepad++ 或 Komodo Edit,那么你可以在窗口底部右侧的状态栏中找到它。
// 我试过了,但没有效果。
在 php 文件的标头中(在所有内容之前)。
<?php header("Content-type: text/html; charset=utf-8"); ?>
在提交的表格中:
<form accept-charset="utf-8" ...>