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

Ele*_*epi 5 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: Ã
?>
Run Code Online (Sandbox Code Playgroud)

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

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

Ele*_*epi 0

正如Mihai所说,我必须删除 utf8_encode() 函数,这会弄乱字符集。取下后,它的工作就如其应有的顺利。