PHP page, MySQL database turn utf-8 garbled, Utf-8 coding problem Summary, Mysqlutf-8
Example one:
PHP page to UTF-8 encoding problem
1. Add a line at the beginning of the 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, to ensure that the file encoding is: UTF-8, if it is ANSI, you need to change the encoding: UTF-8.
3.PHP file Header BOM problem: PHP file must not have BOM label, otherwise, the session can not use the situation, and there are similar hints:
Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent this is because, In the execution of the Session_Start (), the entire page can not have output, but when the previous PHP page has a BOM label, PHP to the 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 use EditPlus to open the file, and in the menu "preferences", "File", "UTF-8 identity", set to: "Always delete signature", and then save the file, you can remove the BOM label!
3.PHP Save the file as an attachment, UTF-8 encoding problem: PHP to save the file as an attachment, the file name must be GB2312 encoding, otherwise, if the file name is in Chinese, it will be garbled: if your PHP itself is UTF-8 encoded format of the file, The file name variable needs to be converted from UTF-8 to Gb2312:iconv ("UTF-8", "GB2312", "$filename");
4. When I truncate the title of the article, there is garbled or "? Question mark:
General article title very long time, will show a part of the title, will be truncated to the title of the article, because a UTF-8 encoding format of the Chinese characters will occupy 3 character width, the interception of the title, sometimes only to a Chinese character 1 characters or 2 character width, not intercepted complete, will appear garbled or "? "The case of a question mark,
Using the following function to intercept the title, 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]) >) $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; }
MySQL database using UTF-8 encoding problem
1. When creating a database with phpMyAdmin database and data table, set the collation to: "Utf8_general_ci" or execute the statement:
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 stored in English or a number, the default is OK.
The corresponding SQL statement, for example:
CREATE TABLE ' test ' (' ID ' INT not NULL, ' name ' VARCHAR (Ten) CHARACTER SET UTF8 COLLATE utf8_general_ci not null, PRIMA RY KEY (' id ')) ENGINE = MYISAM;
2. Read and write databases 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
will be able to read and write the MySQL database properly.
Example two:
The solution to the problem of Utf-8 Chinese garbled in Php+mysql
Summary of issues:
1.mysql database default encoding is UTF8, if this code and your PHP page inconsistent, may cause MySQL garbled.
Creating a table in 2.MYSQL allows you to choose an encoding that, if inconsistent with your page encoding, can cause MySQL to be garbled.
3.MYSQL Add a field when creating a table is optional encoding, if this encoding and your page encoding inconsistent, it can also cause MySQL garbled.
4. User-submitted page encoding and display data page encoding inconsistent, it will certainly cause PHP page garbled.
5. If the user input data page is BIG5 code, display user Input page is gb2312, this 100% will cause PHP page garbled.
The 6.PHP page character set is incorrect.
7.PHP connection MySQL DATABASE statement the specified encoding is incorrect.
Use mysql+php to produce garbled reason all understand very clearly, then solve is not difficult.
Workarounds for different issues:
1.mysql database default encoding is UTF8, if this code and your PHP page inconsistent, may cause MySQL garbled.
Modify the database encoding, if the database encoding is not correct, you can execute the following command in 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 inconsistent with your page encoding, can cause MySQL to be garbled.
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 is optional encoding, if this encoding and your page encoding inconsistent, it can also cause MySQL garbled.
To modify the encoding of a field:
Alter TABLE ' test ' change ' dd ' "dd ' VARCHAR" 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. User-submitted page encoding and display data page encoding inconsistent, it will certainly cause PHP page garbled.
If this is easy to solve, just check the next page, modify the source file charset.
5. If the user input data page is BIG5 code, display user Input page is gb2312, this 100% will cause PHP page garbled.
This situation is also modified page charset can.
The 6.PHP page character set is incorrect.
In order to avoid the PHP page garbled occurrence, the PHP page begins the first sentence
Header ("content-type:text/html; Charset=utf-8 ");
Forcibly specify the encoding of the page to avoid garbled characters
7.PHP connection MySQL DATABASE statement the specified encoding is incorrect.
In the statement that connects to the database.
mysql_connect (' localhost ', ' user ', ' password '); mysql_select_db (' my_db '); mysql_query ("Set names ' UTF8 '"); Gator This sentence after the Select database
The above content is this article to introduce you PHP page, MySQL database to utf-8 garbled, Utf-8 coding problem Summary, I hope you like.
http://www.bkjia.com/PHPjc/1049119.html www.bkjia.com true http://www.bkjia.com/PHPjc/1049119.html techarticle PHP page, MySQL database to utf-8 garbled, Utf-8 coding problem Summary, mysqlutf-8 example one: PHP page to UTF-8 coding problem 1. In the beginning of the code to add a line: Header ("Content-type:tex ...