Php and mysql uft-8 Chinese encoding garbled several solutions _ PHP Tutorial

Source: Internet
Author: User
Php and mysql uft-8 Chinese encoding garbled several solutions. PHP page to UTF-8 encoding problem 1. add a line at the beginning of the code: Copy the code as follows: header (Content-Type: texthtml; charsetutf-8); 2. PHP file encoding problems Click editor PHP page to UTF-8 encoding problems
1. add a line at the beginning of the code:

The code is as follows:


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:


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:


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:


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:


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


Add two rows:

The code is as follows:


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:


$ 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:




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:


Mysql_query ("set names 'gbk '");


The page should also be changed:

The code is as follows:




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.

Lifecycle 1. add a line at the beginning of the code: the code is as follows: header ("Content-Type: text/html; charset = utf-8"); 2. for PHP file encoding problems, click the editor's...

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.