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

How to insert ö/ä/ü in Database using php utf8

我知道它被问了十几次,但我唯一想要的是使用 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,Table 本身的字符集设置为 utf8_general_ci。 MySQL服务器版本为5.5.31 MySQL 字符集是 UTF-8 Unicode

有人知道我做错了什么或者我在这里遗漏了什么吗?

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