PHP queries mysql and Chinese data. different solutions are displayed for the same data added in different ways.

Source: Internet
Author: User
PHP queries mysql and Chinese data. the same data added in different ways shows different hi. let me first talk about my situation. New to php. ---- Phenomena output page, I have added & lt; metacharset = & quot; UTF-8 & quot; & gt;, I am also querying mysql, added mysql_query (& quo PHP queries mysql and Chinese data. the same data added in different ways is displayed differently.
Hi,
Let me explain my situation first. New to php.
---- Symptom
On the output page, I have added When querying mysql, I added
Mysql_query ("set character set utf8 ");
Mysql_query ("set names 'utf8 '");
Some Chinese characters can be displayed, while others cannot.

---- Garbled display process and code
In phpmyadmin, the database and text fields are all set with utf8_unicode_ci.

I manually use phpmyadmin to add Chinese characters to the database, and then directly execute
Mysql_query ("set character set utf8 ");
Mysql_query ("set names 'utf8 '");
$ Rs = $ db-> query ("SELECT * FROM book ");
Garbled characters are displayed.

---- Properly displayed code and unexpected results
Mysql_query ("set character set utf8 ");
Mysql_query ("set names 'utf8 '");
$ Rs2 = $ db-> exec ("insert into 'lal1_k '. 'book' ('bookname', 'bookde') VALUES ('soldier ', 'soldier 小 ')");
$ Rs = $ db-> query ("SELECT * FROM book ");
The inserted data is displayed normally. However, the problem is that when I use phpmyadmin to view the data I inserted, it is garbled.
----
Therefore, I don't know why this problem occurs. Do I need to use code to insert data in the future. Phpmyadmin cannot be used again.


------ Solution --------------------
In phpmyadmin, directly execute set names 'utf8' and then SELECT... what is the result?
Phpmyadmin also requires link encoding.
------ Solution --------------------
Is there a problem with the file encoding?
------ Solution --------------------
It's a virtual host. everything is normal.

The user submits the Utf8 and php operations to the database, and the database can be checked out.

Phpadmin garbled characters are caused by mysql_server encoding. generally, service providers use latin encoding. However, service providers generally ensure that connenct/client/result is utf8, that is, the encoding for data access is utf8, and latin is used for storage, therefore, php operations may be garbled in phpadmin.

In addition, the data inserted by executing SQL statements in phpadmin may be garbled during php reading. this problem is amazing. I didn't see the encoding of the phpmyadmin page.

One sentence: set Library, table, column encoding is all utf8-unicode-ci, after the data in the database are utf8 encoding, do not care about phpadmin query data garbled, will not affect the user can.
------ Solution --------------------
Coding commonly used is utf8-general-ci
It is not just a table that needs to be integrated, but a total of four
First, the database types, including databases, tables, and fields, must be unified. check the fields.
Second, the file encoding type. if you use dw or editplus, you can view the page encoding.
Third, set NAMES utf8 when accessing the database;
4. browser display mode
Otherwise, problems may occur.
Related Article

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.