Many of you may encounter Chinese garbled characters when using commands or phpmyadmin to import data. Next I will introduce you to the solution for importing garbled data. If you need it, refer to it.
Many of you may encounter Chinese garbled characters when using commands or phpmyadmin to import data. Next I will introduce you to the solution for importing garbled data. If you need it, refer to it.
To avoid garbled characters during import, you only need to ensure the encoding of the data file and even the encoding during import. Note: it is not the same as the encoding. For example, the data file data. SQL, encoded as a UTF-8, uses the following command:
The Code is as follows: |
|
Mysql-uroot-p -- default-character-set = utf8 db <data. SQL |
The above method may not be effective, because we do not know mysql encoding, We can first view mysql Encoding
The Code is as follows: |
|
Mysql> show variables like "% char % "; + -------------------------- + ------------------------------------------------------------------------------- + | Variable_name | Value | + -------------------------- + ------------------------------------------------------------------------------- + | Character_set_client | latin1 | | Character_set_connection | latin1 | | Character_set_database | utf8 | | Character_set_filesystem | binary | | Character_set_results | latin1 | | Character_set_server | utf8 | | Character_set_system | utf8 | | Character_sets_dir |/usr/local/Percona-Server-5.1.57-rel12.8-233 -- x86_64/share // charsets/| + -------------------------- + ------------------------------------------------------------------------------- + 8 rows in set (0.01 sec) Mysql> show variables like "% coll % "; + ---------------------- + ------------------- + | Variable_name | Value | + ---------------------- + ------------------- + | Collation_connection | latin1_swedish_ci | | Collation_database | utf8_bin | | Collation_server | utf8_bin | + ---------------------- + ------------------- + |
Modify an SQL File
The Code is as follows: |
|
SET collation_connection = utf8_bin; SET character_set_client = utf8; SET character_set_connection = utf8; |
Then execute Import
The Code is as follows: |
|
Mysql -- socket =/opt/mydata/my3306/my3306.sock-A <xxx. SQL is fine... |
We also found a solution to program import garbled code, that is, add a set names UTF8 after mysql_connect to eliminate garbled Code. For GBK databases, use set names gbk. The Code is as follows:
The Code is as follows: |
|
$ Mysql_mylink = mysql_connect ($ mysql_host, $ mysql_user, $ mysql_pass ); Mysql_query ("set names 'gbk '");
|
The database character set is UTF-8.
The connection statement uses this
The Code is as follows: |
|
Mysql_query ("set names 'utf8 '"); Mysql_query ("set character set UTF8 "); Mysql_query ("SET CHARACTER_SET_RESULTS = utf8 '");
|