Php and mysql uft-8 Chinese encoding garbled several solutions

Source: Internet
Author: User
Php developers may have encountered the issue of garbled text. The following describes how to solve garbled text in php file garbled text and page garbled text and phpmysql database connection.

Php developers may have encountered the issue of garbled text. The following describes how to solve garbled text in php file garbled text and page garbled text and php mysql database connection.

PHP page to UTF-8 encoding problems

1. add a line at the beginning of the code: header ("Content-Type: text/html; charset = utf-8 ");

2. php file encoding problem, click the editor menu: "File"-> "save as", you can see the current file encoding, ensure that the file encoding is: UTF-8, if it is ANSI, you need to change the encoding to: UTF-8.

3. php file header BOM problem: the php file cannot have the BOM label. Otherwise, the session cannot be used, and a similar prompt is displayed: Warning: session_start () [function. session-start]: Cannot send session cache limiter-headers already sent

This is because, when executing session_start (), the entire page cannot be output. However, because the former PHP page has a BOM label, PHP regards this BOM label as output, therefore, an error occurs. Therefore, you must delete the BOM label on the PHP page.

To delete this BOM tag:

(1). you can use Dreamweaver to open the file and save it again to remove the BOM tag!

(2 ). you can open the file with EditPlus, and in the menu "preferences"-> "files"-> "UTF-8 identifier", set to "always delete signature", and then save the file, you can remove the BOM label!

4. PHP in the form of attachments to save the file, the UTF-8 encoding problem: PHP in the form of attachments to save the file, the file name must be GB2312 encoding, otherwise, if the file name contains Chinese words, it will display garbled: if your PHP itself is a UTF-8-encoded file, you need to convert the file name variable from the UTF-8 to GB2312:

Iconv ("UTF-8", "GB2312", "$ filename ");

How to use programs to intercept instance characters

  1. Function utf8_substr ($ str, $ len)
  2. {
  3. For ($ I = 0; $ I <$ len; $ I ++)
  4. {
  5. $ Temp_str = substr ($ str, 0, 1 );
  6. If (ord ($ temp_str) & gt; 127 ){
  7. $ I ++;
  8. If ($ I <$ len ){
  9. $ New_str [] = substr ($ str, 0, 3 );
  10. $ Str = substr ($ str, 3 );
  11. }
  12. } Else {
  13. $ New_str [] = substr ($ str, 0, 1 );
  14. $ Str = substr ($ str, 1 );
  15. }
  16. }
  17. Return join ($ new_str );
  18. }
Issues with using UTF-8 encoding for MYSQL databases

1. use phpmyadmin to create databases and data tables. when creating a database, set "sorting" to "utf8_general_ci" or run the following statement:

Create database 'dbname' default character set utf8 COLLATE utf8_general_ci;

When creating a data table: if this field is for Chinese characters, you need to set "sorting" to "utf8_general_ci". if this field is for English or numbers, by default.

The corresponding SQL statement, for example:

  1. Create table 'test '(
  2. 'Id' int not null,
  3. 'Name' VARCHAR (10) character set utf8 COLLATE utf8_general_ci not null,
  4. Primary key ('id ')
  5. ) ENGINE = MYISAM;

2. use PHP to read and write the database. after connecting to the database:

[Hide] $ connection = mysql_connect ($ host_name, $ host_user, $ host_pass );

Add two rows:

  1. Mysql_query ("set character set 'utf8'"); // read the database
  2. Mysql_query ("set names 'utf8'"); // write database

Can be normal read and write MYSQL database, with the appserv-win32-2.5.10 environment, when installing this package with the default utf8 encoding, when writing the database connection file, written:

  1. $ Conn = mysql_connect ("$ host", "$ user", "$ password ");
  2. Mysql_query ("set names 'utf8 '");
  3. Mysql_select_db ("$ database", $ conn );

Then pay attention to this sentence when you make the page:

In this way, the utf8 page is generated by default in DW CS4, if it is written:

Mysql_query ("set names 'gbk '");

The page should also be changed:

In conclusion, we can solve the garbled problem easily by unifying the page encoding, especially when setting the set names in mysql_query (), it is necessary to make statistics on the page and database encoding.

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.