Several solutions to uft-8 Chinese code garbled in PHP and MySQL

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags click code code follows coding content create default delete

For garbled this problem almost all of the PHP developers have encountered, we are mainly introduced below the PHP file garbled and page garbled and PHP MySQL database connection garbled solution.
-
-
PHP page UTF-8 encoding problem
1. Add a line to the beginning of the code:

The code is as follows
Header ("Content-type:text/html;charset=utf-8");

2.PHP file Encoding Problem

Click on the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file is encoded as: UTF-8,
If ANSI, you need to change the encoding to: UTF-8.
3.PHP file Header BOM problem:
The PHP file must not have a BOM label
Otherwise, the session cannot be used and has a similar hint:
Warning:session_start () [function.session-start]: Cant send session cache limiter-headers already sent
This is because when the session_start () is executed, the entire page cannot have output, but when a BOM tag exists on the previous PHP page,
PHP is the BOM label as output, so it went wrong!
So the PHP page must delete the BOM label
Delete this BOM label method:
1. You can open the file with Dreamweaver and save it again, that is, you can remove the BOM label!
2. You can open the file with EditPlus, and in the menu "preferences"-> "file"-> "UTF-8 identity", set to: "Always delete signature",
Then save the file, that is, you can remove the BOM label!
4.PHP Save the file as an attachment, the UTF-8 encoding problem:
PHP saves the file as an attachment, and the filename must be GB2312 encoded,
Otherwise, if the file name in Chinese, will be displayed garbled:
If your PHP itself is a file in UTF-8 encoded format,
You need to convert the filename variable from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$filename");

Using program to instance character interception method

The code is as follows
function Utf8_substr ($STR, $len)
{
for ($i =0; $i < $len; $i + +)
{
$temp _str=substr ($str, 0, 1);
if (Ord ($temp _str) > 127) {
$i + +;
if ($i < $len) {
$new _str[]=substr ($str, 0, 3);
$str =substr ($STR, 3);
}
}else {
$new _str[]=substr ($str, 0, 1);
$str =substr ($STR, 1);
}
}
return join ($new _str);
}


The problem of using UTF-8 encoding in MySQL database

1. Create databases and datasheets with phpMyAdmin
When you create a database, set the collation to: Utf8_general_ci
Or execute the statement:

The code is as follows
CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;


When you create a datasheet: If the field is for Chinese, you need to set the collation to: Utf8_general_ci.

If the field is stored in English or a number, the default is OK.

The corresponding SQL statement, for example:

The code is as follows
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:

The code is as follows
[hide] $connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:

The code is as follows
mysql_query ("Set character set ' UTF8 '");/Read Library

mysql_query ("Set names ' UTF8 ')"//write Library

You can read and write the MySQL database normally.


Use the appserv-win32-2.5.10 to do the environment, when loading this package with the default UTF8 encoding.
When writing a database connection file, write:

The code is as follows
$conn = mysql_connect ("$host", "$user", "$password");
mysql_query ("SET NAMES ' UTF8 '");
mysql_select_db ("$database", $conn);

And then when you do the page, pay attention to this sentence:

The code is as follows
<meta http-equiv= "Content-type" content= "HTML; Charset=utf-8 "/>

This will be normal if the input database is in Chinese or the page is displayed.
In the DW CS4 Edition, the default generation is also the UTF8 page.

Similarly, if you start by writing a database connection file:

The code is as follows
mysql_query ("SET NAMES ' GBK '");

The page should also be changed accordingly:

The code is as follows
<meta http-equiv= "Content-type" content= html; charset=gb2312 "/>

Summary, finally, the main page coding to be unified can be very convenient to solve the garbled problem, especially in mysql_query () the Set names settings must and the page and the database code statistics one can.

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.