Example 1:
Converting 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 problem. Click the menu of the editor: "File"->"Save As", you can see the encoding of the current file. Make sure the file encoding is: UTF-8. If it is ANSI, the encoding needs to be Change to: UTF-8.
3. PHP file header BOM problem: PHP files must not have BOM tags, otherwise, the session will not be usable and there will be a similar prompt:
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 Since the BOM tag exists in the previous PHP page, PHP treats the BOM tag as output, so an error occurs! Therefore, the BOM tag must be deleted from the PHP page
How to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it to remove the BOM tag!
2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signature", and then save the file. Remove the BOM tag!
3. UTF-8 encoding problem when PHP saves a file as an attachment: When PHP saves a file as an attachment, the file name 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 encoding format. You need to convert the file name variable from UTF-8 to GB2312: iconv("UTF-8", "GB2312", "$filename");
4. When truncating and displaying the article title, garbled characters or "?" question marks appear:
Generally, when the title of an article is very long, part of the title will be displayed and the title of the article will be truncated. Since a Chinese character in UTF-8 encoding format will occupy 3 characters in width, when the title is intercepted, sometimes only the If the width of a Chinese character is 1 character or 2 characters, if it is not completely intercepted, garbled characters or "?" question marks will appear,
Use the following function to intercept the title and there will be 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]) > 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; }
The problem of using UTF-8 encoding in MYSQL database
1. Use phpmyadmin to create the database and data table. When creating the database, please set "Organization" 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 stores Chinese, you need to set "Organization" to: "utf8_general_ci". If the field stores English or numbers, the default is fine.
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. Use PHP to read and write the database
After connecting to the database:
$connection = mysql_connect($host_name, $host_user, $host_pass);
Add two lines:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
You can read and write the MYSQL database normally.
Example 2:
Solution to the utf-8 Chinese garbled problem of php mysql
Summary of questions:
1. The default encoding of mysql database is utf8. If this encoding is inconsistent with your PHP web page, it may cause MYSQL garbled code.
2. When creating a table in MYSQL, you will be asked to choose an encoding. If this encoding is inconsistent with your web page encoding, it may also cause MYSQL garbled code.
3. You can choose the encoding when adding fields when creating a table in MYSQL. If this encoding is inconsistent with the encoding of your web page, it may also cause MYSQL garbled code.
4. The encoding of the page submitted by the user is inconsistent with the encoding of the page displaying the data, which will definitely cause the PHP page to be garbled.
5. If the page where the user inputs information is in big5 code, but the page where the user input is displayed is in gb2312, this will 100% cause the PHP page to be garbled.
6. The character set of the PHP page is incorrect.
7. The encoding specified in the PHP connection MYSQL database statement is incorrect.
Now that we have a clear understanding of the reasons for garbled characters when using mysql php, the solution will not be difficult.
Solutions to different problems:
1. The default encoding of mysql database is utf8. If this encoding is inconsistent with your PHP web page, it may cause MYSQL garbled code.
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 your web page encoding, it may also cause MYSQL garbled code.
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. You can choose the encoding when adding fields when creating a table in MYSQL. If this encoding is inconsistent with the encoding of your web page, it may also cause MYSQL garbled code.
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 field encoding of dd in the test table to utf8.
4. The encoding of the page submitted by the user is inconsistent with the encoding of the page displaying the data, which will definitely cause the PHP page to be garbled.
如果是这种情况容易解决,只需检查下页面,修改源文件的charset即可.
5.如用户输入资料的页面是big5码, 显示用户输入的页面却是gb2312,这种100%会造成PHP页面乱码.
这种情况也是修改页面charset即可.
6.PHP页面字符集不正确.
为了避免PHP页面乱码的发生,PHP页面开始第一句
header("content-type:text/html; charset=utf-8");
//强行指定页面的编码,以避免乱码
7.PHP连接MYSQL数据库语句指定的编码不正确.
在连接数据库的语句中.
mysql_connect('localhost','user','password'); mysql_select_db('my_db'); mysql_query("set names 'utf8'"); //select 数据库之后加多这一句
以上内容就是本文给大家介绍php页面,mysql数据库转utf-8乱码,utf-8编码问题总结,希望大家喜欢。