如何使用php utf8在数据库中插入ö/ä/ü

时间:2015-01-30 18:30:28

标签: php mysql sql database utf-8

我知道,它被问了十几次,但我唯一想要的就是使用php脚本将一个像'Ä'这样的德文字母插入到数据库中,使用现有的解决方案无法正常工作。

<?php
header('Content-type: text/html; charset=utf-8'); 
$con = mysqli_connect("localhost", "username", "password");

mysqli_set_charset($con, "utf8mb4");
mysqli_select_db($con, "database");
mysqli_query($con, "SET SESSION CHARACTER_SET_RESULTS =utf8mb4;");
mysqli_query($con, "SET SESSION CHARACTER_SET_CLIENT =utf8mb4;");
mysqli_query($con, "SET NAMES 'utf8mb4';");

mysqli_query($con,
"SET character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8';");

$title = 'Ö';
$result = mysqli_query($con, 
"INSERT INTO Test (Title)
VALUES ('" . utf8_encode($title) . "');");
#Doesn't work, this was inserted: Ã
?>

“Title”列的字符集设置为utf8mb4_general_ci,表格本身设置为utf8_general_ci。 MySQL Server版本是5.5.31 MySQL Charset是UTF-8 Unicode

有人知道我做错了什么,或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

正如Mihai所说,我不得不删除utf8_encode()函数,这会使字符集混乱。移除后,它可以正常工作。