Home  >  Article  >  Database  >  How to solve garbled characters when interacting with Excel and MySQL

How to solve garbled characters when interacting with Excel and MySQL

WBOY
WBOYforward
2023-06-03 12:04:591153browse

1. Reasons

1. Inconsistent character sets: Excel’s default character set is GB2312, while MySQL’s default character set is UTF-8 or GBK , which may cause data to be garbled during transmission.

2. Data type mismatch: VARCHAR and TEXT type fields in MySQL can store different character sets, while cells in Excel only support one character set. If Excel cells contain different character sets, garbled characters may appear.

3. Inconsistent file encoding: When using Excel to open or save files in CSV format, encoding inconsistencies may occur. For example, if ANSI encoding is selected when saving a CSV file, and MySQL uses UTF-8 encoding, the data may be garbled during transmission.

2. Solution

In order to avoid garbled characters caused by inconsistent character sets when connecting to MySQL, you need to set the character set to GB2312 to keep the character set consistent. The following parameters can be added to MySQL Connector/ODBC:

charset=GB2312

2. Data type matching: When creating a MySQL data table, VARCHAR and TEXT type fields can be specified as GB2312 character set. For example:

CREATE TABLE test (
id INT NOT NULL,
name VARCHAR(30) CHARACTER SET GB2312 DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

To keep the file encoding consistent, UTF-8 encoding should be selected when using Excel to save CSV files. For example, you can save the file in Excel by clicking "File" -> "Save As" and selecting the "CSV UTF-8 (Comma Delimited)" format.

In addition, sometimes you can also use the SET NAMES command to set the character set in MySQL. For example:

SET NAMES 'GB2312'.

The above is the detailed content of How to solve garbled characters when interacting with Excel and MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Does mysql have arrays?Next article:Does mysql have arrays?