'Can't insert Chinese character into MySQL

The cookie is encoded using the big5 set, and it cannot insert into MySQL. Could you help me to solve this problem?

Fields: username is eng, date1 is date, reason1 is a Chinese character.

$reason1 = $_COOKIE["reason"];
$sql2="INSERT INTO 
    attendance_count(username,date,count_time,appendix) 
    VALUES ('$username','$date1','0','$reason1')";
mysql_query($sql2);


Solution 1:[1]

For Chinese, you really need utf8mb4, not utf8. This is because of a small number of Chinese characters that require 4-byte UTF8 characters, which MySQL's utf8 does not support.

Solution 2:[2]

The real problem is about the cookie being encoded big5? Is the rest of the data in the client encoded as utf8? If 'yes' to both, ...

Change the connection to be big5, do the INSERT/SELECT of the cookie, then change back to utf8 (or, better, utf8mb4 after upgrading).

Executing SET NAMES big5 or calling the charset('big5') function for the API you are using is the preferred way to switch back and forth.

(Meanwhile, get away from the mysql_* interface; switch to mysqli_* or PDO.)

If everything in the client is big5, then there is no need to switch back and forth. Simply declare that the client is using big5. Note: It does not matter whether the table/column is also big5; MySQL will convert between them.

Solution 3:[3]

update character set of table columns as well with utf8 along with table character set. May be check in Mysql workbench alter table and see character set of columns you want to support Chinese characters.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick James
Solution 2 Rick James
Solution 3 phvish