PHP page, MySQL database to turn utf-8 garbled, Utf-8 coding problem Summary _php Example

Source: Internet
Author: User
Tags create database phpmyadmin truncated

Example one:

PHP page UTF-8 encoding problem

1. In the code begins to add a line: Header ("Content-type:text/html;charset=utf-8");

2.PHP file Encoding Problem click the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file encoding is: UTF-8, if it is ANSI, you need to change the code to: UTF-8.

3.PHP file Header BOM problem: PHP file must not have BOM label, otherwise, will appear session can not use the situation, and have similar hints:

Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent this is because, In the execution of Session_Start (), the entire page can not have output, but when the previous PHP page exists BOM label, PHP to this BOM label as output, so the error! So the PHP page must delete the BOM label

How to delete this BOM label:

1. You can use Dreamweaver to open the file and save it again, that is, you can remove the BOM label!

2. You can use EditPlus to open the file, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete the signature", and then save the file, that is, you can remove the BOM label!

3.PHP as an attachment to save the file, UTF-8 coding problem: PHP as an attachment to save the file, file name must be GB2312 code, otherwise, if the file name in Chinese, it will be displayed garbled: If your PHP itself is UTF-8 encoded format files, File name variables need to be converted from UTF-8 to Gb2312:iconv ("UTF-8", "GB2312", "$filename");

4. When the title of the article is truncated, there is garbled or "? "Question mark:

General article title for a long time, will show a part of the title, will be truncated to the title of the article, because a UTF-8 encoded format of the Chinese characters will occupy 3 character width, the interception of the title, sometimes only intercepted to a Chinese character of 1 characters or 2 character width, did not intercept the complete, will appear garbled or "? "The question Mark's case,

Using the following function to intercept the caption, there is no problem:

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; 
}

The problem of using UTF-8 encoding in MySQL database

1. When creating databases and datasheets with phpMyAdmin, set the collation to: "Utf8_general_ci" or execute the statement:

CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

When you create a datasheet: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci", if the field is stored in English or numbers, the default is OK.

The corresponding SQL statement, for example:

CREATE TABLE ' test ' ( 
' id ' INT NOT NULL, 
' name ' VARCHAR "CHARACTER SET UTF8 COLLATE utf8_general_ci NOT null , 
PRIMARY KEY (' id ') 
) ENGINE = MYISAM;

2. Read and write database in PHP

After connecting to the database:

$connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:

mysql_query ("Set character set ' UTF8 '");/Read library 
mysql_query ("Set names ' UTF8 '");//write Library

You can read and write the MySQL database normally.

Example two:

The solution of Php+mysql utf-8 Chinese garbled problem

Problem Summary:

1.mysql database default encoding is UTF8, if this code and your PHP page is inconsistent, may cause MySQL garbled.

Creating a table in 2.MYSQL allows you to choose an encoding that, if it is inconsistent with your Web page encoding, can also cause MySQL garbled code.

3.MYSQL Add a field when creating a table can choose to encode, if this encoding and your Web page encoding inconsistent, may also cause MySQL garbled.

4. The user submits the page the code and displays the data the page code to be inconsistent, will certainly cause the PHP page garbled.

5. If the user input data page is BIG5 code, display user Input page is gb2312, this 100% will cause the PHP page garbled.

The 6.PHP page character set is incorrect.

7.PHP connection The MySQL database statement specifies an incorrect encoding.

Use mysql+php produce garbled reason all know very clearly, then solve is not difficult.

Solutions for different problems:

1.mysql database default encoding is UTF8, if this code and your PHP page is inconsistent, may cause MySQL garbled.

Modify the database encoding, if the database encoding is incorrect, you can execute the following command at phpMyAdmin:

Alter DATABASE ' test ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_bin

The above command is to set the test database encoding to UTF8.

Creating a table in 2.MYSQL allows you to choose an encoding that, if it is inconsistent with your Web page encoding, can also cause MySQL garbled code.

To modify the encoding of a table:

Alter TABLE ' category ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_bin

The above command is to change the encoding of a table category to UTF8.

3.MYSQL Add a field when creating a table can choose to encode, if this encoding and your Web page encoding inconsistent, may also cause MySQL garbled.

To modify the encoding of a field:

Alter TABLE ' test ' change ' dd ' DD ' VARCHAR (a) CHARACTER SET UTF8 COLLATE utf8_bin not NULL

The above command is to change the field encoding of DD in the test table to UTF8.

4. The user submits the page the code and displays the data the page code to be inconsistent, will certainly cause the PHP page garbled.

If this situation is easy to solve, just check the page, modify the source file charset can be.

5. If the user input data page is BIG5 code, display user Input page is gb2312, this 100% will cause the PHP page garbled.

This situation is also modify the page charset can be.

The 6.PHP page character set is incorrect.

In order to avoid the PHP page garbled occurrence, PHP page began the first sentence

Header ("content-type:text/html; Charset=utf-8 ");

Forcibly specify the encoding of the page to avoid garbled

7.PHP connection The MySQL database statement specifies an incorrect encoding.

In the statement that connects to the database.

mysql_connect (' localhost ', ' user ', ' password ');
mysql_select_db (' my_db ');
mysql_query ("Set names ' UTF8 '"); After the Select database Cadogan this sentence

The above content is this article introduces the PHP page to everybody, the MySQL database turns utf-8 garbled, the utf-8 coding question Summary, hoped everybody likes.

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.