Why does PHP / MySQL insert my Chinese characters differently?

Gday all

I have a problem when trying to insert some Chinese characters into my MySQL database from PHP using mysqlnd.

I have a form that takes some details, like Internal name, External name, Snapshot name, etc.

I entered "语言 测试" (language testing) in all three fields on the form.

I submit my information using an internal connection, for example:

UPDATE table1 INNER JOIN table2(table1.name = "value1", table2.ext_name = "value2", table2.ext_name = "value3")

If both tables and fields are set to utf8_general_ci (I also tried utf8_bin)

The insert works correctly, however, I see two values ​​inserted into the database.

In the first table I see "è¯è¨ € æμ <è¯ •", and in table 2 I see "语言 测试".

What could cause my insertion of exactly the same data from the same php form to appear differently in two separate MySQL database tables?

+3
source share
2 answers

In MySQL, you not only need to set the character encoding of the table (or its columns), but you must set the character encoding of your connection between PHP and the database, which runs every time you connect.

In PHP you can use mysql_set_charset(), or if you use PDO include charset=UTF-8in the name of the data source.

This replaces (and PHP recommends this in favor) SET NAMESin MySQL. In older versions of PHP, you used the MySQL command SET NAMES UTF8every time you connected.

+2
  • UTF-8 .

    (BOM)

  • MySQL :

    SET NAMES UTF-8
    SET CHARACTER SET UTF-8
    

    10.1.5.

  • .htaccess :

    AddDefaultCharset utf-8
    AddCharset utf-8 *
    <IfModule mod_charset.c>
      CharsetSourceEnc utf-8
      CharsetDefault utf-8
    </IfModule>
    

    htaccess

+3

Source: https://habr.com/ru/post/1759264/


All Articles