Php page, mysql database to UTF-8 garbled, UTF-8 encoding problem summary, mysqlutf-8
Example 1:
PHP page to UTF-8 Encoding Problems
1. Add a line at the beginning of the Code: header ("Content-Type: text/html; charset = UTF-8 ");
2. PHP file encoding problem click Editor menu: "file"-> "Save as", you can see the current file encoding, ensure that the file encoding: UTF-8, if it is ANSI, you need to change the encoding: UTF-8.
3. php file header BOM problem: 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 sentThis is because, when executing session_start (), the entire page cannot be output. However, because the former PHP page has a BOM label, PHP regards this BOM label as output, so an error occurred! 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 in the menu "Preferences"-> "Files"-> "UTF-8 identifier", set to "always Delete Signature", and then save the file, you can remove the BOM label!
3. PHP in the form of attachments to save the file, the UTF-8 encoding problem: PHP in the form of attachments to save the file, the file name must be GB2312 encoding, otherwise, if the file name contains Chinese words, it will display garbled: if your PHP itself is a UTF-8-encoded file, you need to convert the file name variable from the UTF-8 to GB2312: iconv ("UTF-8", "GB2312", "$ filename ");
4. garbled characters or "?" are displayed when the title of the document is truncated. Question mark:
General article title is very long, will display part of the title, will be truncated, because a UTF-8 encoding format of Chinese characters will occupy 3 Characters in width, when intercepting the title, sometimes only one or two characters in width of one Chinese character are captured. If the width is not completely captured, garbled characters or "?" Question mark,
You can use the following function to extract the title:
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])> 128)
$ 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;
}
Problems with MYSQL database using UTF-8 encoding
1. Use phpmyadmin to create the database and data table. When creating the database, please set "organize" to: "utf8_general_ci" or execute the statement:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
When creating the data table: If the field is stored in Chinese, you need to set "organize" to: "utf8_general_ci". If the field is stored in English or numbers, the default is fine.
The corresponding SQL statement, for example:
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. Read and write database with 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
You can read and write MYSQL database normally.
Example two:
php + mysql utf-8 Chinese garbled solution
Summary of issues:
1.The default encoding of the mysql database is utf8. If this encoding is inconsistent with your PHP web page, it may cause MYSQL garbled characters.
2.When creating a table in MYSQL, you will be asked to choose an encoding.If this encoding is inconsistent with the encoding of your web page, it may also cause MYSQL to garble.
3.MYSQL can add encoding when creating a table.If this encoding is inconsistent with the encoding of your web page, it may also cause MYSQL to be garbled.
4.The encoding of the page submitted by the user is not consistent with the page encoding of the displayed data, which will definitely cause the PHP page to be garbled.
5.If the page where the user enters the information is big5, but the page where the user enters is gb2312, this 100% will cause the PHP page to be garbled.
6.PHP page character set is incorrect.
7.The encoding specified by the PHP connection MYSQL database statement is incorrect.
The cause of garbled characters using mysql + php is well understood, so it is not difficult to solve it.
Solutions for different problems:
1.The default encoding of the mysql database is utf8. If this encoding is inconsistent with your PHP web page, it may cause MYSQL garbled characters.
Modify the database encoding. If the database encoding is incorrect, 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 encoding of the test database to utf8.
2.When creating a table in MYSQL, you will be asked to choose an encoding.If this encoding is inconsistent with the encoding of your web page, it may also cause MYSQL to garble.
Modify the encoding of the 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 can add encoding when creating a table.If this encoding is inconsistent with the encoding of your web page, it may also cause MYSQL to be garbled.
Modify the encoding of the field:
Alter TABLE 'test' CHANGE 'dd' 'dd' VARCHAR (45) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
The above command is to change the dd field encoding in the test table to utf8.
4.The encoding of the page submitted by the user is not consistent with the page encoding of the displayed data, which will definitely cause the PHP page to be garbled.
If this is easy to solve, just check the page and modify the charset of the source file.
5.If the page where the user enters the information is big5, but the page where the user enters is gb2312, this 100% will cause the PHP page to be garbled.
In this case, you can modify the page charset.
6.PHP page character set is incorrect.
In order to avoid garbled PHP pages, the first sentence of the PHP page begins
header ("content-type: text / html; charset = utf-8");
// Force specify the encoding of the page to avoid garbled characters
7.The encoding specified by the PHP connection MYSQL database statement is incorrect.
In a statement that connects to the database.
mysql_connect ('localhost', 'user', 'password');
mysql_select_db ('my_db');
mysql_query ("set names 'utf8'"); // Add this sentence after selecting the database
The above content is this article to introduce you to the php page, mysql database to UTF-8 garbled, UTF-8 encoding problem summary, I hope everyone likes it.