3

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?

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
Nyendra
  • 33
  • 4
  • Consider using an integer id as primary key instead. Or use utf8_bin if you really need that field to be unique. – Phil Oct 21 '15 at 12:28

3 Answers3

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?

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

Community
  • 1
  • 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.)

friedemann_bach
  • 1,418
  • 14
  • 29
  • Thank you, I changed the collation of both the table and field to utf8_bin, and after that inserting of both records work. – Nyendra Oct 21 '15 at 12:32
0

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.

Andreas Linden
  • 12,489
  • 7
  • 51
  • 67