PHP + MYSQL garbled solution _ PHP Tutorial

Source: Internet
Author: User
PHP + MYSQL garbled solution. All of them have solved the issue of garbled code in JSP, but some people still haven't solved the problem of MYSQL garbled code, including myself, so I found some information, I hope this time we can completely solve all the garbled characters in JSP, but some people still haven't solved the problem of MYSQL garbled characters, including myself, so I found some information, we hope this time we can completely solve the problem of garbled characters in the MYSQL database.
The first method is an incisive summary:
The VM is often changed, and the MYSQL versions of different service providers are different. after data is imported, garbled characters and other problems cannot be displayed normally. I checked a lot of information and summarized my skills:

This is the case when importing data in WINDOWS.
Use MYSQL commands
Run the doscommand to enter the mysql bin directory and enter the mysql-uroot-p password database name. <要恢复的数据库, 例如我们要把d盘的一个名称为test.sql的数据库恢复到本地的test2这个数据库,那么就这样: mysql -uroot -p密码 test2以前的国外主机用的mysql是4.x系列的,感觉还比较好,都无论gbk和utf-8都没有乱码,没想到新的主机的mysql是5.0版本的,导入数据后,用php读出来全是问号,乱码一片,记得我以前也曾经有过一次切换出现乱码的经验,原因肯定是mysql版本之间的差异问题。

I had to check the information and found a solution. I added a set names UTF8 sentence after mysql_connect to eliminate garbled characters in the UTF8 database. for GBK databases, I used 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.

The connection statement uses this
Mysql_query ("set names 'utf8 '");
Mysql_query ("set character set UTF8 ");
Mysql_query ("SET CHARACTER_SET_RESULTS = utf8 '");

Another way is to change the MYSQL character set if your machine is running. it is generally valid in MYSQL 4 and MYSQL 5.


Method 2: a good explanation. try it.

Summary of mysql garbled processing:

(1) Chinese characters are processed normally in java. displaying garbled characters in the cmd client is a problem with character sets.

(2) the length of the field is long enough. However, when a Chinese character is inserted, the system prompts com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column.

(3) garbled characters are the final issue of character sets. we should consider the following aspects from character set settings: server, client, database, connection, and results.

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

(1) modify the my. ini (MySQL Server Instance Configuration file)

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.