PHP / MySQL中的字符编码(i /î/ï)

时间:2022-05-21 21:37:29

I'm having troubles with accents on the character i (ï/î) to be put into a MySQL database using PHP. I've 2 names, Myîa and Myïa, and after the inserts I only have Myïa in my database.

我正在使用PHP将角色i(ï/î)的重音放入MySQL数据库时遇到麻烦。我有2个名字,Myîa和Myïa,插入后我只在我的数据库中有Myïa。

The collation of the table is set to utf8_unicode_ci and the field itself is also utf8_unicode_ci. I've checked the DEFAULT_CHARACTER_SET_NAME in the SCHEMATA table which is set to utf8 for the database.

表的排序规则设置为utf8_unicode_ci,字段本身也是utf8_unicode_ci。我检查了SCHEMATA表中的DEFAULT_CHARACTER_SET_NAME,该表设置为数据库的utf8。

database.php:

database.php中:

$dbs_cnt = mysqli_connect($dbs_hst, $dbs_usr, $dbs_psw, $dbs_nme);
if (!$dbs_cnt) { die(mysqli_connect_error()); }
if (!mysqli_set_charset($dbs_cnt, "utf8")) { echo mysqli_error($dbs_cnt); }

script.php

script.php的

header('Content-Type: text/html; charset=utf-8');
include 'database.php';
// irrelevant code
$SQL = "
    INSERT INTO " . $dbs_tbl . " (`Character`)
    VALUES ('" . $CHR_NME . "')
";
if (!mysqli_query($dbs_cnt, $SQL)) { echo mysqli_error($dbs_cnt); }

Everything seemed to be working fine until I came across these names recently.

在我最近遇到这些名字之前,一切似乎都运转良好。

When trying to manually enter them in the database when one record is already present, I am getting the following errors:

当一个记录已经存在时尝试在数据库中手动输入它们时,我收到以下错误:

#1062 - Duplicate entry 'Myîa' for key 'PRIMARY' 
#1062 - Duplicate entry 'Myïa' for key 'PRIMARY' 

I've been playing around with the collation (as it was latin1_swedish_ci), but that doesn't seem to help. After checking similar problems I've added the header aswell, but with no result either.

我一直在玩整理(因为它是latin1_swedish_ci),但这似乎没有帮助。在检查了类似的问题后,我还添加了标题,但也没有结果。

What am I missing?

我错过了什么?

3 个解决方案

#1


1  

Set the collation to "utf8_bin". The glyphs i / ï / î are identical in "utf8_unicode_ci". (Or choose another collation that treats those letters as distinct.)

将排序规则设置为“utf8_bin”。字形i /ï/î在“utf8_unicode_ci”中是相同的。 (或者选择另一种将这些字母视为不同的排序规则。)

#2


1  

I can not comment , so I send this link as an answer. A similar problem of mine is solved with the link below. Change MySQL default character set to UTF-8 in my.cnf?

我无法发表评论,所以我发送此链接作为答案。我的类似问题通过以下链接解决。在my.cnf中将MySQL默认字符集更改为UTF-8?

sudo gedit /etc/mysql/my.cnf

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
skip-character-set-client-handshake
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8

I restarted the system and it worked (maybe a restart of mysql can be sufficent).

我重新启动系统,它工作(也许重启mysql可以足够)。

#3


0  

Rigth after connecting to mysql, execute the following query: SET NAMES utf8.

连接到mysql后,执行以下查询:SET NAMES utf8。

This is requried if you want to send UTF8 data to the server.

如果要将UTF8数据发送到服务器,则需要这样做。

#1


1  

Set the collation to "utf8_bin". The glyphs i / ï / î are identical in "utf8_unicode_ci". (Or choose another collation that treats those letters as distinct.)

将排序规则设置为“utf8_bin”。字形i /ï/î在“utf8_unicode_ci”中是相同的。 (或者选择另一种将这些字母视为不同的排序规则。)

#2


1  

I can not comment , so I send this link as an answer. A similar problem of mine is solved with the link below. Change MySQL default character set to UTF-8 in my.cnf?

我无法发表评论,所以我发送此链接作为答案。我的类似问题通过以下链接解决。在my.cnf中将MySQL默认字符集更改为UTF-8?

sudo gedit /etc/mysql/my.cnf

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
skip-character-set-client-handshake
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8

I restarted the system and it worked (maybe a restart of mysql can be sufficent).

我重新启动系统,它工作(也许重启mysql可以足够)。

#3


0  

Rigth after connecting to mysql, execute the following query: SET NAMES utf8.

连接到mysql后,执行以下查询:SET NAMES utf8。

This is requried if you want to send UTF8 data to the server.

如果要将UTF8数据发送到服务器,则需要这样做。