Home  >  Article  >  Backend Development  >  Solution to garbled characters in PHP+MYSQL_PHP Tutorial

Solution to garbled characters in PHP+MYSQL_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:50:28860browse

The garbled code problem in JSP must have been solved, but there are still some people whose MYSQL garbled code problem has not been solved, including myself, so I looked for some more information, hoping that this time I can completely solve the garbled code problem in the MYSQL database.
The first method, a very incisive summary:
Virtual hosts are often changed, and the MYSQL versions of each service provider are different. When the data is imported, there will always be problems such as garbled characters that cannot be displayed normally. I checked a lot of information. , summed up some of my own skills:

The import under WINDOWS should be like this
Use the MYSQL command
Enter the bin directory of mysql under the DOS command, enter mysql -uroot -p password database name< ;The database to be restored, for example, we want to restore a database named test.sql in the D drive to the local test2 database, then it is like this: mysql -uroot -p Password The Mysql used by the foreign host before test2 was 4. The x series, I feel better, there is no garbled code in both GBK and UTF-8. Unexpectedly, the Mysql of the new host is version 5.0. After importing the data, when I read it with Php, it is full of question marks and garbled code. I remember that I used to I once had the experience of garbled characters when switching. The reason must be the difference between Mysql versions.

I had to check the information and found a solution, which is to add SET NAMES UTF8 after mysql_connect to eliminate garbled characters in the UTF8 database. For GBK database, use SET NAMES GBK. 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

Use this for the connection statement
mysql_query("SET NAMES 'UTF8'");
mysql_query("SET CHARACTER SET UTF8");
mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");

Another method is that if you have your own machine, you can change the MYSQL character set, which is generally valid in the two versions of MYSQ4 and MYSQL5


The second method: Very good explanation, you can try it

Summary of mysql garbled code processing:

(1) Chinese characters are processed normally in java, and garbled characters are displayed in cmd client because of the character set problem.

(2) The field length is set long enough, but when inserting Chinese characters, the error com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column is prompted. The error is a character set problem.

(3) The garbled code problem is ultimately a character set problem, so consider the character set settings, which are nothing more than the following aspects: server, client, database, connection, results.

------- ------------------Solution------------------------------

(1 )Modify my.ini (MySQL Server Instance Configuration file)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319322.htmlTechArticleThe garbled code problem in JSP must have been solved, but there are still some people whose MYSQL garbled code problem has not been solved, including I myself, so I looked for some more information, hoping to fully understand this time...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn