PHP Writing database Chinese garbled problem

Source: Internet
Author: User

Disclaimer: This article from http://www.jb51.net/article/30123.htm

PHP page to UTF-8 encoding problem

1. Add a line at the beginning of the code:

<span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >2.php file encoding problem  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Click on the Editor's menu: "File", "Save as" to see the current file encoding, to ensure that the file is encoded as: utf-8, </span><br style= "Font-family:tahoma, Arial , Song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > If it is ANSI, you need to change the encoding to: UTF-8.  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >3.php file Header BOM problem:  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >php file must not have BOM label &Nbsp;</span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Otherwise, the session will not be able to use the situation, and there are similar hints:  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent < /span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > 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,  </span><br style= "Font-family:tahoma , Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >php this BOM label asIt's an output, so it's a mistake!  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > So php page must delete BOM label  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Delete this BOM label method:  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >1. You can use Dreamweaver to open files and resave them, that is, you can remove BOM labels!  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >2. You can open the file with EditPlus, and in the menu "preferences", "File", "UTF-8 identity", set to: "Always delete signature",  </span><br style= " Font-family:tahoma, Arial, Song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Then save the file, that is, the BOM label can be removed!  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >4.php in the form of attachments to save the file, UTF-8 coding problem:  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >php Save the file as an attachment, the filename must be GB2312 encoded,  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Otherwise, if the file name has Chinese words, it will be garbled:  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; Font-size: 14px; line-height:25.2px; " > If your PHP itself is UTF-8 encoded format file,  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Need to change the file name variable from UTF-8 to Gb2312: </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " >iconv ("UTF-8", "GB2312", "$filename"),  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Using the program to capture the character of the instance  </span>
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:

<span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > When creating a data table: If the field is in Chinese, then you need to set the collation to: "Utf8_general_ci", </span><br style= "Font-family:tahoma, Arial, song body ; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > If the field is in English or a number, it will be the default. </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > corresponding SQL statements, for example: </span>
2. Read and write databases in PHP
after connecting to the database:
add two lines:
<span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ></span></span><pre name= "code" class= "PHP" >mysql_query ("set character set ' UTF8 '");//Read Library Mysql_ Query ("Set names ' UTF8 '");//write library//actually read and write can only add mysql_query ("Set names ' UTF8 '")

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:
then, when making the page, pay attention to this sentence:
<span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px;" ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; Background-color:rgb (221, 237, 251); " ><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>&NBSP;</SPAN></SPAN></SPAN></SPAN> 
<span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px;" ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ><span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > This is normal regardless of the input database's Chinese, or the page display.  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > in DW version CS4, the default generated is also the UTF8 page.  </span><br style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; "/><span style=" Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " > Similarly, if you write a database connection file at the beginning:  </span></span></span></span><pre name= "code" class= " PHP ">mysql_query (" SET NAMES ' GBK '); 
The page should also be changed accordingly:




<span style= "Font-family:tahoma, Arial, song body; font-size:14px; line-height:25.2px; " ></span>





PHP Writing database Chinese garbled problem

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.