PHP and MySQL Transit UTF8 coding issues Summary, MySQL relay UTF8 encoding _php Tutorial

Source: Internet
Author: User
Tags php and mysql

PHP and MySQL Transit UTF8 coding issues Summary, MySQL relay UTF8 encoding


If a website needs internationalization, it needs to convert the code from GB2312 to UTF-8, which has a lot of problems to note, if there is no conversion thoroughly, there will be a lot of coding problems appear!
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 files must not have BOM labels, otherwise, the session can not be used, and there are similar tips: Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent this is because, in the execution of Session_Start (), the entire page cannot have output, but when the previous PHP page has a BOM label, 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", and then save the file, you can remove the BOM label!
3.PHP UTF-8 encoding problem when saving files as attachments:PHP to save the file as an attachment, the filename must be GB2312 encoding, otherwise, if the file name is in Chinese, it will be garbled: if your PHP itself is a UTF-8 encoded format file, you need to change the filename variable 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 "? "Question mark, if you use the following function to intercept the title, 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]) >)         $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

It is hoped that this article is helpful to everyone's php+mysql program design.

http://www.bkjia.com/PHPjc/1060097.html www.bkjia.com true http://www.bkjia.com/PHPjc/1060097.html techarticle php and MySQL relay UTF8 coding problem Summary, MySQL relay UTF8 encoding a website if internationalization is required, you need to convert the code from GB2312 to UTF-8, which has a lot of problems to be aware of, such as ...

  • Related Article

    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.