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

时间:2022-09-26 08:47:07

It was asked a dozen times, I know, but the only thing I want is to insert a German letter like 'Ä' into a database using a php script which simply doesn't work using the existing solutions.

我知道,它被问了十几次,但我唯一想要的是使用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: Ã
?>

The charset of the column 'Title' is set to utf8mb4_general_ci and of the Table itself its utf8_general_ci. MySQL Server version is 5.5.31 MySQL Charset is UTF-8 Unicode

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

Does anybody know what I am doing wrong or am I missing something here?

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

1 个解决方案

#1


0  

As Mihai said, I had to remove the utf8_encode() function, which messed the charset up. After removing it, it works as smooth as it should.

正如Mihai所说,我不得不删除utf8_encode()函数,这会使charset搞砸了。在移除它之后,它可以像它应该的那样平滑。

#1


0  

As Mihai said, I had to remove the utf8_encode() function, which messed the charset up. After removing it, it works as smooth as it should.

正如Mihai所说,我不得不删除utf8_encode()函数,这会使charset搞砸了。在移除它之后,它可以像它应该的那样平滑。