PHP Chinese garbled (from Baidu Library) (remember: Anyway, use Navicat to create a database when you choose Utf-8 encoding.)

Source: Internet
Author: User
Tags mysql client mysql code mysql in php script readfile

PHP Chinese garbled is one of the common problems in PHP development. PHP Chinese garbled sometimes occurs in the Web page itself, some generated in the process of MySQL interaction, and sometimes related to the operating system. Here's a summary.

A The first is the coding of PHP pages

The best and quickest solution is that the page statement is encoded in accordance with the internal code of the database, if the page number of pages requested is inconsistent with the internal code of the database, set the connection code, mysql_query ("SET NAMES XXX"); XXX is a connection code. It will solve the problem of garbled characters.

1. php file itself and the encoding of the Web page should match a. If you want to use gb2312 encoding, then PHP to output headers: header ("content-type:text/html; charset=gb2312 "), static page add <meta http-equiv=" Content-type "content=" text/html "; charset=gb2312 "", all files are encoded in ANSI, can be opened with Notepad, save as a selection encoding for ANSI, overwriting the source file. B. If you want to use UTF-8 encoding, then PHP to output headers: header ("content-type:text/html; Charset=utf-8 "), static page add <meta http-equiv=" Content-type "content=" text/html "; Charset=utf-8 "", all files are encoded in the format of Utf-8. Save As Utf-8 may be a bit of trouble, general Utf-8 file at the beginning will have a BOM, if the session will be a problem, you can use EditPlus to save, in EditPlus, tool-> parameter selection-> file->utf-8 signature, select the total is deleted, then save the BOM information can be removed.

2. PHP itself is not Unicode, all functions such as substr have to be changed to MB_SUBSTR (need to install mbstring extension), or iconv transcoding.

Two PHP's data interaction with MySQL

PHP and database coding should be consistent

1. Modify MySQL configuration file My.ini or my.cnf,mysql best UTF8 encoding [MySQL] default-character-set=utf8 [mysqld] Default-character-set=utf8 Default-storage-engine=myisam under [mysqld] Add: Default-collation=utf8_bin init_connect= ' SET NAMES UTF8 '

2. In the need to do database operation of the PHP program before adding mysql_query ("Set Names ' code ')", Encoding and PHP code consistent, if the PHP code is gb2312 that MySQL code is gb2312, if it is utf-8 that MySQL code is UTF8, this will not appear garbled when inserting or retrieving data

Three PHP and operating system-related Windows and Linux encoding is not the same, in the Windows environment, the invocation of PHP functions when the parameters are Utf-8 encoding error, such as Move_uploaded_file (), FileSize (), ReadFile (), these functions are often used when processing uploads and downloads, and the following errors may appear when invoked: Warning:move_uploaded_file () [function.move-uploaded-file]:failed To open stream:invalid argument. 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.. In the Linux environment with GB2312 encoding although these errors will not appear, but the saved file name will not be able to read the file, then the parameters can be converted to the operating system to identify the code, encoding conversion can be used mb_convert_encoding (strings, new code, the original code) or Iconv (original code, new Code, string), so that after processing the saved file name will not appear garbled, you can read the file normally, to achieve the Chinese name file upload, download. In fact, there are better solutions, completely disconnected from the system, you do not have to consider the system is what the code. You can generate a sequence of letters and numbers as a file name, and the original with Chinese name in the database, so call Move_uploaded_file () will not have problems, download the file name only to the original with the Chinese name. Implement the downloaded code 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 type of file, $file _name is the original name, $file _path is the address of the file that is saved on the service.

Four Let's summarize why it's garbled. In general, there are 2 reasons for the occurrence of garbled code, first of all, because of the coding (charset) Set error, resulting in the browser to the wrong code to parse, resulting in a full screen messy "heavenly book", followed by the file is the wrong code to open, and For example, a text file was originally GB2312 encoded, but it was opened and saved with UTF-8 encoding. To solve the above garbled problem, first need to know which links in the development of the Code:

1, file encoding: Refers to the paging file (. html,.php, etc.) itself is the type of code to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page, so there is no problem. But Zendstudio will not automatically recognize the code, it will only be fixed according to the preferences of the configuration of a code to open the file, if the work is not noticed, with the wrong code to open the file, made a change after a save, garbled appeared (I have a deep experience).

2, page statement code: In the HTML code head inside, you can use <meta http-equiv= "Content-type" content= "text/html;" charset= "xxx"/> To tell the browser what encoding the Web page, the current Chinese web site in the development of XXX is mainly used in the GB2312 and UTF-8 two kinds of coding. 3, database Connection code: Refers to the database operation when the encoding and database transmission data, here need to pay attention to the database itself is not confused with the coding, such as MySQL internal default is latin1 encoding, that is, MySQL is latin1 encoded to store data, Data transmitted to MySQL in other encodings is converted to latin1 encoding. Know where the web development involved in coding, also know the reason for the garbled code: the above 3 coding settings are inconsistent, because most of the encoding is compatible with ASCII, so the English symbol will not appear, Chinese on the bad luck.

Five Decisive battles some common error situations and solutions:

1, the database uses UTF8 code, and the page affirms the code is GB2312, this is the most common generation of garbled reason. At this time in the PHP script directly select data out is garbled, need to use before the query: mysql_query ("SET NAMES GBK"); 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: mysql_query ("SET NAMES UTF8"); Attention is UTF8 rather than 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's data input and output is more complex than the above, the MySQL configuration file My.ini defines 2 default encodings, respectively, in [client] Default-character-set and [mysqld] Default-character-set to set the default client connection and the code used inside the database. The code we specify above is actually a MySQL Client Connection server command line parameter character_set_client, to tell the MySQL server received the client data is what encoding, rather than the default encoding.

2, page declaration code and the file itself is inconsistent with the code, this situation rarely occurs, because if the code inconsistent with the page when the browser to see is garbled. More often it is after the release to modify some small bugs, to open the page in error encoding and then save the resulting. Or with some FTP software directly online modify files, such as CuteFTP, due to software coding configuration errors caused by the conversion of the wrong encoding. 3, some rented virtual host friends, obviously the above 3 codes are set up correctly or garbled. For example, the Web page is GB2312 code, ie, such as browser Open but always recognized as UTF-8, the page head has been declared 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 code, in Httpd.conf added Adddefaultcharset UTF-8. At this time the server will first send HTTP headers to the browser, its priority than the page in the Declaration of High Code, the natural browser to identify the wrong. There are 2 solutions, ask the administrator to add a adddefaultcharset GB2312 to the configuration file's own virtual machine to override the global configuration, or configure it in the. htaccess of your own directory. Summary: In short, to solve the problem to solve the PHP Chinese garbled best solution is that the page statement code and the database internal code is consistent, if the page number of pages requested and the database internal coding inconsistent, set the connection code, mysql_query ("Set NAMES XXX"); XXX is a connection code. It will solve the problem of garbled characters.

3.navicat can not display Chinese correctly, execute set NAMES UTF8 can display Chinese correctly.

Six, commonly used to set the encoding mode.

1, first database library, table and fields are used UTF8 save.

2. The database set NAMES UTF8 is set to UTF8 connection mode. Perform show variables like ' char% ', and view character_set_client as UTF8 is set to complete.

3. PHP executes the mysql_query ("SET NAMES UTF8") before executing the query statement, and then executes the query statement.

4, the page encoding for GB2312, the query out of the string with Iconv ("UTF-8", "Gbk//ignore", the Chinese character string) after conversion, and then display. If the page is encoded as UTF-8, it can be displayed directly.

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.