1

I am trying to make a data for dictionary.

But when system save transcribe to DB, it loses some of the special character.

Ex:

group /ɡruːp/

When it saves it in DB, it just retains group /ɡrup/

It loses : character.

What character set can save this value?

I tried with UTF8,UTF8-MB4.

Here is my PHP code:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("UTF-16LE");

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$stmt = $conn->prepare("INSERT INTO words (WORD,WORD_UPPER_CASE, TRANSCRIBE, DESCRIPTION)VALUES (?, ?, ?,?)");
$stmt->bind_param("ssss", $_WORD,$_WORD_UPPER_CASE, $_TRANSCRIBE, $_DESCRIPTION);

// set parameters and execute
$_WORD = $WORD;
$_WORD_UPPER_CASE = $WORD_UPPER_CASE;
$_TRANSCRIBE = $TRANSCRIBE;
$_DESCRIPTION = $DESCRIPTION;

$stmt->execute();   
$stmt->close();
$conn->close();

Thanks.

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
Lam Son
  • 87
  • 1
  • 1
  • 9
  • thats only the description, you'll need to provide the code that does the insertion – Kevin Mar 23 '16 at 04:04
  • What encoding is the input data in? `UTF-16LE`...?! That's rather unusual... – deceze Mar 23 '16 at 09:05
  • You don't check whether `->set_charset()` succeeds; I don't think it does. In fact, you don't check for errors anywhere except in connection; that's not good practice. – Álvaro González Mar 23 '16 at 09:23

1 Answers1

1

Your code assumes that declaring the encoding cannot go wrong:

$conn->set_charset("UTF-16LE");

If you run var_dump($conn->set_charset("UTF-16LE")) you'll see bool(false) because that's not a valid encoding name in MySQL.

In the See Also section of the function documentation you can find a List of character sets that MySQL supports link. In my system, UTF encodings include these:

mysql> SHOW CHARACTER SET LIKE '%utf%';
+---------+----------------+--------------------+--------+
| Charset | Description    | Default collation  | Maxlen |
+---------+----------------+--------------------+--------+
| utf8    | UTF-8 Unicode  | utf8_general_ci    |      3 |
| utf8mb4 | UTF-8 Unicode  | utf8mb4_general_ci |      4 |
| utf16   | UTF-16 Unicode | utf16_general_ci   |      4 |
| utf32   | UTF-32 Unicode | utf32_general_ci   |      4 |
+---------+----------------+--------------------+--------+
4 rows in set (0.00 sec)

Last but not least, you shouldn't be trying out random encodings. Your application charset is something that you decide. In 2016 there're little reasons to not use UTF-8.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks for your answer. So I try to setting utf8mb4 in DB and in PHP code set charset is utf8mb4 but still error – Lam Son Mar 23 '16 at 10:17
  • 1
    Please check [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through). Apart from telling the system that you are using UTF-8, you need to actually use it. – Álvaro González Mar 23 '16 at 10:37