创建表时使用UTF-8.
create table table_name () CHARACTER SET = utf8;
插入表时使用UTF-8
set username utf8; INSERT INTO table_name (ABC,VAL);
详细代码
下面代码是测试代码,请执行以下工作.
如果您已经创建了数据库,请将其更改为以下代码.
如果您没有创建数据库,则创建它并将Collation设置为utf8_unicode_ci
对于表格,将collation定义为“utf8_unicode_ci”,用于存储中文字符的表字段.
ALTER DATABASE `stackoverflow` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE `stackoverflow`.`chines`
(
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`words` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE = InnoDB;
define('HOSTNAME', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'stackoverflow');
$dbLink = mysql_connect(HOSTNAME, USERNAME, PASSWORD)or die(mysql_error());
mysql_query("SET character_set_results=utf8", $dbLink)or die(mysql_error());
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db(DATABASE, $dbLink)or die(mysql_error());
mysql_query("set names 'utf8'",$dbLink)or die(mysql_error());
if(isset($_POST['addWord']))
{
mysql_query("SET character_set_client=utf8", $dbLink)or die(mysql_error());
mysql_query("SET character_set_connection=utf8", $dbLink)or die(mysql_error());
$sql_query = "INSERT INTO chines(words) VALUES('".$_POST['words']."')";
mysql_query($sql_query, $dbLink)or die(mysql_error());
}
?>
mysql_query("SET character_set_results=utf8", $dbLink);
$sql_query = "SELECT * FROM chines";
$dbResult = mysql_query( $sql_query, $dbLink)or die(mysql_error());
?>
Word :
while($row = mysql_fetch_assoc($dbResult))
{
echo $row['words']. '
';
}
?>
查看结果和步骤,如下所示.