PHP MySQL Chinese garbled solution, the database display is normal, PHP call is not normal

Source: Internet
Author: User
Tags php mysql php script blank page mysql command line

In general, garbled appearance there are 2 reasons, the first is due to the encoding (charset) set error, causing the browser to parse with the wrong encoding, resulting in a full screen of a messy "heavenly book", followed by the wrong code to open the file, and then save, such as a text file was originally GB2312 encoded, It is opened and saved with UTF-8 encoding. To solve the above garbled problem, you need to know what aspects of development involve coding:

1, file encoding: Refers to the page file (. html,.php, etc.) itself is what encoding to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page and are therefore less problematic. And Zendstudio does not automatically identify the code, it will only be fixed in accordance with the preferences of the configuration of a certain encoding to open the file, if the working time a note, with the wrong code to open the file, made a change after the save, garbled appear.

2, page declaration code: In the HTML code head inside, you can use <meta http-equiv= "Content-type" content= "text/html; charset= "XXX"/> (this sentence must be written in front of <title>XXX</title>, otherwise it will lead to a blank page (ie+php only)) to tell the browser page what encoding to use, At present the Chinese website development mainly uses the GB2312 and the UTF-8 two kinds of coding.

3, database Connection code: Refers to the database operation in which encoding and database transfer data, it is important to note that the database itself is not confused with the code, such as MySQL internal default is latin1 encoding, that is, MySQL is latin1 encoding to store data, Data transmitted to MySQL in other encodings is converted to latin1 encoding.

Know the Web development where the coding, also know the reason for garbled: The above 3 encoding settings are inconsistent, because the majority of the encoding is compatible with ASCII, so the English symbol will not appear, Chinese is unlucky. Here are some common error conditions to resolve:

1, the database using UTF8 encoding, and page declaration code is GB2312, which is the most common cause of garbled. At this time in the PHP script directly inside the Select data is garbled, need to be used before the query:

mysql_query ("SET NAMES GBK");
or mysql_query ("SET NAMES GB2312");

To set the MySQL connection code to ensure that the page declaration code is consistent with the connection code set here (GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use:

Note that it is a UTF8 rather than a general UTF-8. If the code of the page declaration is consistent with the internal encoding of the database, the connection code can be set.
Note: In fact, MySQL data input and output is more complicated than the above, the MySQL configuration file My.ini defined 2 default encoding, respectively [client] in the Default-character-set and [mysqld] Default-character-set to set the default client connection and the encoding used internally by the database. The code we specified above is actually the command line parameter character_set_client of the MySQL client to connect to the server, to tell the MySQL server what encoding the client data is accepted, not the default encoding.

2, the page declaration code and the file itself encoding inconsistent, this situation rarely occurs, because if the code is inconsistent with the page when the browser sees is garbled. More often than not after the release of some minor bugs, the wrong code to open the page and save the resulting. or using some FTP software to directly modify the file online, such as CuteFTP, due to the software encoding configuration error, resulting in the wrong conversion encoding.

3, some rented virtual host friends, clearly the above 3 codes are set correctly or garbled. For example, the Web page is GB2312 encoded, IE and other browser open but always recognized as UTF-8, the page head has been declared to be GB2312, manually modify the browser code for GB2312 after the page display normal. The reason is that the server Apache set the server global default encoding, in Httpd.conf added Adddefaultcharset UTF-8. At this time the server will first send HTTP headers to the browser, the priority is higher than the page stated that the code is high, the natural browser is identified wrong. There are 2 solutions, please add a adddefaultcharset GB2312 to the config file's own virtual machine to cover the global configuration, or configure it in the. htaccess of your own directory.

garbled resolution Method

In order to solve the garbled problem, we must first understand what code the database uses. If not specified, it will be the default latin1.
The most we should use is the 3 character set Gb2312,gbk,utf8.
So how do we go about specifying the character set of the database? Below also GBK for example
"CREATE database in MySQL Command line client"
mysql> CREATE TABLE ' Mysqlcode ' (
' ID ' TINYINT (255) UNSIGNED not NULL auto_increment PRIMARY KEY,
' Content ' VARCHAR (255) Not NULL
) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> desc Mysqlcode;
+---------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+----------------+
| ID | tinyint (255) unsigned | NO |         PRI | | auto_increment |
| Content | varchar (255) |     NO |         |                | |
+---------+-----------------------+------+-----+---------+----------------+
2 rows in Set (0.02 sec)

where the following type = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
is the character set of the specified database, COLLATE (collation), which allows MySQL to support multiple encoded databases at the same time.
Of course, we can also modify the database character set by the following instructions
ALTER DATABASE da_name default character set ' CharSet '.
The client is sent in GBK format and can be configured as follows:
SET character_set_client= ' GBK '
SET character_set_connection= ' GBK '
SET character_set_results= ' GBK '
This configuration is equivalent to SET NAMES ' GBK '.
Now for the database operation that you just created
mysql> use test;
Database changed
mysql> INSERT INTO mysqlcode values (null, ' PHP enthusiast ');
ERROR 1406 (22001): Data too long for column ' content ' at row 1
No specified character set is GBK and an error occurred while inserting
mysql> set names ' GBK ';
Query OK, 0 rows affected (0.02 sec)
Specifies that the character set is GBK
mysql> INSERT INTO mysqlcode values (null, ' PHP enthusiast ');
Query OK, 1 row Affected (0.00 sec)

Insert Successful

Mysql> select * from Mysqlcode;
+----+-----------+
| ID | Content |
+----+-----------+
| 1 | PHP Hobby |
+----+-----------+
1 row in Set (0.00 sec)

Reading can also be garbled when no character set GBK is specified, as follows

Mysql> select * from Mysqlcode;
+----+---------+
| ID | Content |
+----+---------+
| 1 |  Php??? |
+----+---------+
1 row in Set (0.00 sec)

"Create a database in phpMyAdmin and specify the character set"



Table type according to their own needs to choose, here MyISAM (support full-text search);
Collation Select Gbk_chinese_ci is the GBK character set
Gbk_bin Simplified Chinese, binary. Gbk_chinese_ci Simplified Chinese, case insensitive.

Insert the database in the database you just created



Again browsing when found to be garbled


Why is it? Because the database is the GBK character set, and we do not specify it as GBK
Back to the database homepage


You can see the MySQL connection proofing the default Latin1_bin. We'll change it to gbk_chinese_ci.


Insert a second piece of data. Look, this one's normal.



"Troubleshoot PHP read database garbled"

Still take database Mysqlcode as an example <?php $conn = mysql_connect ("localhost", "root", "89973645"); mysql_query ("Set names ' GBK '");//This is the specified database character set, usually placed behind the connection database is mysql_select_db ("test"); $sql = "SELECT * from Mysqlcode"; $result = mysql_query ($sql, $conn); ?>  <body> <table width= "height=" border= "1" align= "center" cellpadding= "0" cellspacing= "0" > <tr > &LT;TD width= align= "center" >id</td> <td width= "229" align= "center" > Content </td> </tr > <?php while ($row = Mysql_fetch_assoc ($result)) {echo "<tr> <td align=/" center/">". $row [' ID '] ." </td> <td> ". $row [' content ']." </td> </tr> "; }?> </table> </body>

Plus that's normal again.


One word
What code do you use in your database, set names ' your code ' before operating on the database;

PHP MySQL Chinese garbled solution, the database display is normal, PHP call is not normal

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.