Php and mysql uft-8 Chinese encoding garbled several solutions

Source: Internet
Author: User
Tags character set php file php and php and mysql create database

PHP page to UTF-8 encoding problems
1. Add a line at the beginning of the code:

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

2. Php file encoding problems

Click the editor menu: "File"-> "save as", you can see the current file encoding, make sure the file encoding: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3. Php file header BOM problems:
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 sent
This is because, when executing session_start (), the entire page cannot be output, but because the former PHP page has a BOM tag,
PHP regards this BOM label as output, so 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, removing the BOM tag!
2. You can open the file with EditPlus and choose "preferences"> "files"> "UTF-8 identifier" from the menu to "always remove signature ",
Save the file to remove the BOM label!
4. UTF-8 coding problems when PHP saves files in the form of attachments:
PHP saves the file as an attachment. The file name must be GB2312 encoded,
Otherwise, if the file name contains Chinese characters, garbled characters will be displayed:
If your PHP is a UTF-8-encoded file,
You need to convert the file name variable from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$ filename ");

How to use programs to intercept instance characters

 

The code is as follows: Copy code
Function utf8_substr ($ str, $ len)
{
For ($ I = 0; $ I <$ len; $ I ++)
{
$ Temp_str = substr ($ str, 0, 1 );
If (ord ($ temp_str) & gt; 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 );
}


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 execute the statement:

The code is as follows: Copy code
Create database 'dbname' default character set utf8 COLLATE utf8_general_ci;


When creating a data table: if this field is used to store Chinese characters, set "sorting" to "utf8_general_ci ",

This field can be used by default if it is an English or number.

The corresponding SQL statement, for example:

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

The code is as follows: Copy code
[Hide] $ connection = mysql_connect ($ host_name, $ host_user, $ host_pass );

Add two rows:

The code is as follows: Copy code

Mysql_query ("set character set 'utf8'"); // read the database

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

You can read and write the MYSQL database normally.


The environment made of appserv-win32-2.5.10, when installing this package with the default utf8 encoding.
When writing a database connection file, write it:

The code is as follows: Copy code
$ Conn = mysql_connect ("$ host", "$ user", "$ password ");
Mysql_query ("set names 'utf8 '");
Mysql_select_db ("$ database", $ conn );

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

The code is as follows: Copy code
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>

In this way, no matter whether the database is entered in Chinese or displayed on the page, it will be normal.
In DW CS4, the utf8 page is generated by default.

Similarly, if it is written:

The code is as follows: Copy code
Mysql_query ("set names 'gbk '");

The page should also be changed:

The code is as follows: Copy code
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

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.