Php writes mysql Chinese garbled characters, which is utf8.

Source: Internet
Author: User
Php writes mysql Chinese garbled characters. it Is utf8 and the php file is already UFT8,


 $ Conn = mysql_connect ("localhost", "test", "test"); if (! $ Conn) {die ('could not connect :'. mysql_error ();} // mysql_query ("set test 'utf8'"); mysql_set_charset ("utf8", $ conn); // select the database mysql_select_db ("test ", $ conn); // INSERT data to the table mysql_query ("insert into test (id, myusername) VALUES (2, 'Chinese 2 ')"); // mysql_query ("insert into user (id, myusername) // VALUES (". intval ($ str_id ). ",". $ str_name. ")"); // Close the database connection mysql_close ($ conn );


Configure the database in phpmysql.


Why are you?


Reply to discussion (solution)

Mysql_query ("set names utf8 ");

Is the file code UTF-8?
I don't mean The encoding format of a file.

Php_mysql extension does not have the mysql_set_charset method
Php_mysqli has the mysqli_set_charset method.

In addition, you didn't provide garbled text.

If you have set up both databases, we recommend that you create a new database and try again ..

If you have set up both databases, we recommend that you create a new database and try again ..


I set everything, or? The final result is that all the new ones are playing well, that is, there is a problem with the previous build. haha, for your reference only. good luck to you.

Alter table users character set GBK; // modify the table
Alter table users modify username char (20) character set gbk; // modify the field

I have encountered Chinese garbled characters before. The first is the file encoding format, the second file parsing format, and whether the third Database supports Chinese characters.

Mysql_query ("set names utf8 ");



Thank you for choosing the correct answer.

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.