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

Source: Internet
Author: User
Tags php and mysql
PHP page to UTF-8 encoding problem
1. Add a line at the beginning of the code:
Copy CodeThe code is as follows:
Header ("Content-type:text/html;charset=utf-8");

2.PHP File Encoding Issues
Click on the Editor's menu: "File", "Save As", you can see the current file encoding, to ensure that the file encoding: UTF-8,
In the case of ANSI, you need to change the encoding to: UTF-8.
3.PHP file Header BOM problem:
PHP files must not have BOM labels
Otherwise, the session cannot be used, and there are similar hints:
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 have output, but when the BOM label exists because of the previous PHP page,
PHP put this BOM label as output, so it went wrong!
So php page must delete BOM label
How to delete this BOM label:
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, you can remove the BOM label!
4.PHP UTF-8 encoding problem when saving files as attachments:
PHP saves the file as an attachment, and the filename must be GB2312 encoded.
Otherwise, if there is Chinese in the file name, it will be garbled:
If your PHP itself is a file in UTF-8 encoded format,
The file name variable needs to be converted from UTF-8 to GB2312:
Iconv ("UTF-8", "GB2312", "$filename");
Using the program to take the method of character interception
Copy CodeThe 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);
}

MySQL database using UTF-8 encoding problem

1. Create a database and data table with phpMyAdmin
When you create a database, set the collation to: "Utf8_general_ci"
Or execute the statement:
Copy CodeThe code is as follows:
CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

When creating a data table: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci",
If the field is in English or a number, it will be the default.
The corresponding SQL statement, for example:
Copy CodeThe code is as follows:
CREATE TABLE ' Test ' (
' ID ' INT not NULL,
' Name ' VARCHAR (Ten) CHARACTER SET UTF8 COLLATE utf8_general_ci not NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM;

2. Read and write databases in PHP
After connecting to the database:
Copy CodeThe code is as follows:
$connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:
Copy CodeThe code is as follows:
mysql_query ("Set character set ' UTF8 ');//Read Library
mysql_query ("Set names ' UTF8 '");//write Library

will be able to read and write the MySQL database properly.

Use the appserv-win32-2.5.10 to do the environment, when the package is loaded with the default UTF8 code.
When writing a database connection file, write:
Copy CodeThe code is as follows:
$conn = mysql_connect ("$host", "$user", "$password");
mysql_query ("SET NAMES ' UTF8 '");
mysql_select_db ("$database", $conn);

Then, when making the page, pay attention to this sentence:
Copy CodeThe code is as follows:


This way, regardless of the input database of the Chinese, or the page display, it is normal.
In DW version CS4, the UTF8 page is generated by default.
Similarly, if you write a database connection file at the beginning:
Copy CodeThe code is as follows:
mysql_query ("SET NAMES ' GBK '");

The page should also be changed accordingly:
Copy CodeThe code is as follows:


Summary, the last is mainly the page encoding to be unified can be very convenient to solve garbled problem, especially in mysql_query () This set names settings must and the page and database encoding statistics one can.

http://www.bkjia.com/PHPjc/325350.html www.bkjia.com true http://www.bkjia.com/PHPjc/325350.html techarticle 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:text/html;charset=utf-8"); 2.PHP file encoding Problem click on 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.