Php Chinese garbled solutions

Source: Internet
Author: User
Tags error code mysql client mysql code mysql tutorial php server php and php and mysql php code

First, PHP webpage code

1. The php Tutorial file encoding should match the webpage encoding.

A. if you want to use gb2312 encoding, php needs to output the header: header ("Content-Type: text/html; charset = gb2312 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> to the static page. All files are encoded in ANSI format and can be opened in Notepad, to overwrite the source file.

B. to use UTF-8 encoding, php must output the header: header ("Content-Type: text/html; charset = utf-8 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"> to the static page. The encoding format of all files is UTF-8. It may be a little troublesome to save it as UTF-8. Generally, BOM is generated at the beginning of the UTF-8 file. If session is used, problems may occur. You can use editplus to save it in editplus, tool-> parameter selection-> File-> UTF-8 signature, select always delete, and then save to remove the BOM information.

2. php itself is not Unicode. All functions such as substr must be changed to mb_substr (mbstring extension is required), or iconv transcoding is used.

II. Data interaction between PHP and Mysql

PHP and database tutorial encoding should be consistent

1. Modify the mysql tutorial configuration file my. ini or my. cnf. It is best to use utf8 encoding for mysql.

[Mysql]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8
Default-storage-engine = MyISAM
Add the following under [mysqld:
Default-collation = utf8_bin
Init_connect = 'set NAMES utf8'

2. add mysql_query ("set names 'code'") before the php program that requires database operations. The encoding is consistent with the php code. If the php code is gb2312, the mysql code is gb2312, if it is UTF-8, mysql encoding is utf8, so no garbled characters will appear during data insertion or retrieval.

III. PHP related to the operating system

 

The encoding for Windows and Linux is different. In Windows, if the parameter is UTF-8 encoded when a function of PHP is called, an error occurs, such as move_uploaded_file (), filesize (), and readfile () these functions are often used for processing uploads and downloads. The following errors may occur during calls:

Warning: move_uploaded_file () [function. move-uploaded-file]: failed to open stream: Invalid argument in...

Warning: move_uploaded_file () [function. move-uploaded-file]: Unable to move ''to ''in...

Warning: filesize () [function. filesize]: stat failed for... in...

Warning: readfile () [function. readfile]: failed to open stream: Invalid argument in ..

Although gb2312 encoding in Linux does not produce these errors, the stored file name becomes unreadable due to garbled characters. In this case, you can first convert the parameter to the encoding recognized by the operating system, encoding conversion can be performed using mb_convert_encoding (string, new encoding, original encoding) or iconv (original encoding, new encoding, string). In this way, the stored file name will not contain garbled characters, you can also normally read files to upload and download files with Chinese names.

In fact, there are still better solutions to completely break away from the system, so you don't have to consider the encoding of the system. You can generate a sequence with only letters and numbers as the file name, and store the original Chinese name in the database. In this way, calling move_uploaded_file () will not cause problems, during the download, you only need to change the file name to the original name with Chinese characters. The download code is as follows:

Header ("Pragma: public ");

Header ("Expires: 0 ");

Header ("Cache-Component: must-revalidate, post-check = 0, pre-check = 0 ");

Header ("Content-type: $ file_type ");

Header ("Content-Length: $ file_size ");

Header ("Content-Disposition: attachment; filename =" $ file_name "");

Header ("Content-Transfer-Encoding: binary ");

Readfile ($ file_path );

$ File_type is the file type, $ file_name is the original name, and $ file_path is the address of the file stored on the service


The PHP file must be consistent with the static webpage code.

1. When UTF-8 encoding is used, the PHP file is added before all outputs:

Header ("Content-Type: text/html; charset = utf-8 ");
Add static pages:
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8">.

The encoding format of all files is UTF-8. It may be a bit difficult to save as UTF-8, similar to the WINDOWS notepad software, when saving a file encoded in UTF-8, three invisible characters (0xEF 0xBB 0xBF, BOM-Byte Order Mark) are inserted at the beginning of the file ). It is a string of hidden characters, used for the notepad editor to identify whether the file is encoded in UTF-8. For general files, this will not cause any trouble.

But for PHP, PHP did not consider the BOM issue during design, does not ignore the three characters at the beginning of the UTF-8-encoded file BOM, the BOM is used as a part of the beginning body of the file. Because it must be in the <? Or <? The code after php is executed as PHP code, so the three characters will be output on the page. The display effect depends on the browser, which is generally a blank line or garbled code. Due to the restriction of the COOKIE sending mechanism, the COOKIE cannot be sent to files whose names start with BOM (because PHP has already sent the file header before the COOKIE is sent ), therefore, the logon and logout functions are invalid. All functions dependent on cookies and sessions are invalid.
You can use EmEditor to save it. In EmEditor, save it as-> remove the checkmark before the unicode signature (BOM), and then save it to remove the BOM information.

2. Use gb2312 encoding. Add the following code to the PHP file before all outputs:

Header ("Content-Type: text/html; charset = gb2312 "),
Page add
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

All files are encoded in ANSI format.

II. PHP and database encoding should be consistent
Take Mysql database as an example. Add mysql_query ("set names 'XX'") before the php program that requires database operations. If the php code is gb2312, xx indicates gb2312, for UTF-8, xx is utf8 (utf8 instead of UTF-8), so that no garbled characters will occur during data operations.

In addition, it is recommended that mysql use utf8 encoding to modify the mysql configuration file my. ini or my. cnf.

[Mysql]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8
Default-storage-engine = MyISAM

Add the following under [mysqld:
Default-collation = utf8_bin
Init_connect = 'set NAMES utf8'


Chinese characters output by echo are garbled,
In fact, this problem may occur in various server scripts,
Encoding is still a problem,
Generally, for encoding compatibility considerations, most pages define the page character set as UTF-8.

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
To display Chinese characters properly, you need to convert the encoding method. For example:
Echo iconv ("GB2312", "UTF-8", 'Chinese'); will not garbled
There are other methods, such
Add header ("Content-Type: text/html; charset = gb2312") before php echo ");
Of course, the simplified Chinese page can also be simply,
Change the UTF-8 in <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> to gb2312

 
Strange phenomena encountered in reality,
On the page normally displayed on the local server, if it is uploaded to the server, garbled characters are returned,
I have not carefully considered this reason, because through the iconv function GB2312, UTF-8 for location re-encoding will be normal,
However, it is probably caused by different settings on the PHP server,
Check PHP. INI to solve the problem.

 

Let's take a look at the cause of garbled text.

In general, there are two possible causes for garbled text. The first reason is the incorrect encoding (charset) setting, which leads to the incorrect encoding resolution by the browser, resulting in the "Tianshu", which is full of screens ", secondly, the file is opened with an error code, and then saved, for example, a text file originally GB2312 encoding, but opened with UTF-8 encoding and then saved. To solve the above garbled code problem, you must first know which stages of development involve encoding:

1. File encoding: indicates the encoding of the page file (.html,. php, etc. Notepad and Dreamweaver automatically recognize the file encoding when opening the page, so there is no problem. ZendStudio does not automatically recognize the encoding. It only opens the file with a certain encoding according to the preference configuration. If you do not pay attention to it during work, use the error code to open the file, after the modification, the garbled code will appear as soon as it is saved (I have a deep understanding ).

2. Page declarative encoding: in the HTML code HEAD, you can use <meta http-equiv = "Content-Type" content = "text/html; charset = "XXX"/> to tell the browser web page using what encoding, currently Chinese website development XXX mainly uses GB2312 and UTF-8 two types of encoding.

3. Database connection encoding: it refers to the encoding used to transmit data with the database during database operations. Note that it should not be confused with the database encoding, for example, MySQL uses latin1 encoding by default. That is to say, Mysql uses latin1 encoding to store data. Data transmitted to Mysql using other encoding will be converted to latin1 encoding.
When we know where encoding is involved in WEB development, we also know the cause of garbled code: The three encoding settings are inconsistent, because most of the encodings are compatible with ASCII, so the English symbols will not appear, and Chinese characters will be unlucky.

 

<Html>
<Head>
<Title> </title>
</Head>
<Body>
<? Php
$ Mysql_server_name = 'localhost ';
$ Mysql_username = 'root ';
$ Mysql_password = '000000 ';
$ Mysql_database = 'Lib ';
$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password, $ mysql_database );
$ SQL = "select name, age from mytb ";
Print ($ conn );
$ Rs = mysql_db_query ("lib", "select * from mytb", $ conn );
Print ("
<Br> ");
While ($ row = mysql_fetch_object ($ rs )){
Print ($ row-> name. ":". $ row-> age. "<br> ");
}
Mysql_close ($ conn );
?>

Shown as follows:

Resource id #1
Dd: 54
Ddd: 8
?? : 15
??? : 25
?? : 32
Mysql code: utf8, GBK all tried. Mysql font and command line display are correct.

Question added:

Garbled:

??? : 15
??? : 25
?? : 32
In these rows, the database value is Chinese characters, and the question mark is displayed.

Solution:

In $ rs = mysql_db_query ("lib", "select * from mytb", $ conn );

Add

Mysql_query ("set names gb2312"); or mysql_query ("set names gbk ");

 


5. Battle against common errors and solutions:

1. The database uses UTF8 encoding, while the page declarative encoding is GB2312, which is the most common cause of garbled code. In this case, the SELECT data in the PHP script is garbled. You need to use mysql_query ("set names gbk") before querying to SET the MYSQL connection encoding, ensure that the page declarative encoding is consistent with the connection encoding set here (GBK is an extension of GB2312 ). If the page is UTF-8 encoded, you can use: mysql_query ("set names UTF8 ");
Note that it is UTF8 instead of a general UTF-8. If the encoding stated on the page is consistent with the internal encoding of the database, no connection encoding can be set.

Note: In fact, MYSQL data input and output are more complex than described above. MYSQL configuration file my. ini defines two default encodings, they are default-character-set in [client] and default-character-set in [mysqld] to set the encoding used for client connection and database respectively by default. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server to tell the MYSQL server what encoding the client data is received, rather than the default encoding.

2. The page declarative encoding is inconsistent with the file encoding. This rarely happens, because if the encoding is inconsistent, the attacker will see garbled code in the browser when making the page. More often, it is caused by modifying some minor bugs after the release, opening the page with error code, and saving it. Or you can use some FTP software to directly modify files online, such as CuteFTP. The conversion error is caused by incorrect software encoding.

3. Some friends who rent a VM clearly confirm that the above three codes are correctly set and there are still garbled characters. For example, the web page is GB2312 encoding, IE and other browsers open but always recognized as a UTF-8, the web page HEAD has been declared is GB2312, manually modify the browser code to GB2312 after the page shows normal. The cause is that the server Apache sets the server's global default encoding, added the AddDefaultCharset UTF-8 in httpd. conf. At this time, the server will first send an HTTP header to the browser, which has a higher priority than the declarative encoding in the page, and the natural browser will recognize the error. There are two solutions. The administrator needs to add adddefacharcharset GB2312 to the virtual machine in the configuration file to overwrite the global configuration, or configure it in the. htaccess directory.

 

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.