简体   繁体   中英

Character encoding (i/î/ï) in PHP/MySQL

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.

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 .

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

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.

What am I missing?

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?

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).

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.)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM