Php page, mysql database to UTF-8 garbled, UTF-8 encoding problem summary

Source: Internet
Author: User
If a website needs to be internationalized, it needs to convert the code from GB2312 to a UTF-8, there are many problems to note, if not completely converted, there will be a lot of coding problems! Next, I will share with you the php page through this article. mysql database is converted to UTF-8 garbled characters. example 1 of UTF-8:


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 editor menu: "File"-> "save as", you can see the current file encoding, ensure that the file encoding: UTF-8, if it is ANSI, you need to change the encoding: UTF-8.

3. php file header BOM problem: the php file cannot have BOM labels. 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 sentThis 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, so an error occurred! 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, removing 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!

3. 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 ");

4. garbled characters or "?" are displayed when the title of the document is truncated. Question mark:

General article title is very long, will display part of the title, will be truncated, because a UTF-8 encoding format of Chinese characters will occupy 3 characters in width, when intercepting the title, sometimes only one or two characters in width of one Chinese character are captured. If the width is not completely captured, garbled characters or "?" Question mark,

You can use the following function to extract the title:


function get_brief_str($str, $max_length) { 
  echo strlen($str) . ""; 
  if (strlen($str) > $max_length) { 
    $check_num = 0; 
    for ($i = 0; $i < $max_length; $i++) { 
      if (ord($str[$i]) > 128) 
        $check_num++; 
    } 
    if ($check_num % 3 == 0) 
      $str = substr($str, 0, $max_length) . "..."; 
    else 
      if ($check_num % 3 == 1) 
        $str = substr($str, 0, $max_length +2) . "..."; 
      else 
        if ($check_num % 3 == 2) 
          $str = substr($str, 0, $max_length +1) . "..."; 
  } 
  return $str; 
}


Issues with using UTF-8 encoding for MYSQL databases

1. when using phpmyadmin to create a database and a data table to create a database, set "sorting" to "utf8_general_ci" or execute the 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:


CREATE TABLE `test` ( 
`id` INT NOT NULL , 
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY ( `id` ) 
) ENGINE = MYISAM ;


2. use PHP to read and write databases

After connecting to the database:


$connection = mysql_connect($host_name, $host_user, $host_pass);


Add two rows:


Mysql_query ("set character set 'utf8'"); // read database mysql_query ("set names 'utf8'"); // write database


You can read and write the MYSQL database normally.

Example 2:

Php + mysql UTF-8 Chinese garbled problem solution

Problem summary:

1. the default encoding of mysql database is utf8. if this encoding is inconsistent with your PHP webpage, it may cause MYSQL garbled characters.

2. when creating a table in MYSQL, you will be asked to select an encoding. if this encoding is inconsistent with your webpage encoding, it may also cause MYSQL garbled characters.

3. you can select encoding to add fields when creating a MYSQL table. if this encoding is inconsistent with your webpage encoding, MYSQL may also be garbled.

4. if the encoding of the page submitted by the user is inconsistent with the page encoding of the displayed data, it will certainly cause PHP page garbled characters.

5. if the page for user input is big5, the page for user input is gb2312. this 100% will cause PHP page garbled characters.

6. incorrect PHP page character set.

7. the encoding specified by the PHP connection to the MYSQL database statement is incorrect.

The reasons for using mysql + php to produce garbled characters are clearly understood, so it is not difficult to solve the problem.

Solutions to different problems:

1. the default encoding of mysql database is utf8. if this encoding is inconsistent with your PHP webpage, it may cause MYSQL garbled characters.

Modify the database encoding. if the database encoding is incorrect, run the following command in phpmyadmin:


Alter DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin


The preceding command sets the encoding of the test database to utf8.

2. when creating a table in MYSQL, you will be asked to select an encoding. if this encoding is inconsistent with your webpage encoding, it may also cause MYSQL garbled characters.

Modify the table encoding:


Alter TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin



The preceding command changes the category encoding of a table to utf8.

3. you can select encoding to add fields when creating a MYSQL table. if this encoding is inconsistent with your webpage encoding, MYSQL may also be garbled.

Modify the field encoding:


Alter TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL



The preceding command is to encode the dd field in the test table to utf8.

4. if the encoding of the page submitted by the user is inconsistent with the page encoding of the displayed data, it will certainly cause PHP page garbled characters.

If this is the case, you only need to check the page and modify the charset of the source file.

5. if the page for user input is big5, the page for user input is gb2312. this 100% will cause PHP page garbled characters.

In this case, modify the charset page.

6. incorrect PHP page character set.

To avoid PHP page garbled characters, the first sentence of the PHP page begins.


header("content-type:text/html; charset=utf-8");



// Forcibly specify the page encoding to avoid garbled characters

7. the encoding specified by the PHP connection to the MYSQL database statement is incorrect.

In the database connection statement.


mysql_connect ('localhost', 'user', 'password');
mysql_select_db ('my_db');
mysql_query ("set names 'utf8'"); // Add this sentence after selecting the database 


The above content is the summary of the UTF-8 encoding problem on the php page, mysql database conversion to UTF-8, and I hope you will like it.

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.