Home > Backend Development > PHP Tutorial > Several solutions to garbled Chinese encoding of UFT-8 in PHP and MySQL_PHP Tutorial

Several solutions to garbled Chinese encoding of UFT-8 in PHP and MySQL_PHP Tutorial

WBOY
Release: 2016-07-21 15:19:14
Original
1033 people have browsed it

Problems with converting PHP pages to UTF-8 encoding
1. Add a line at the beginning of the code:

Copy the code The code is as follows:

header("Content-Type: text/html;charset=utf-8");

2.PHP file encoding problem
Click the editor menu: "File"-> ;"Save as", you can see the encoding of the current file, make sure the file encoding is: UTF-8,
If it is ANSI, you need to change the encoding to: UTF-8.
3. PHP file header BOM issue:
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 when the BOM tag exists in the previous PHP page,
PHP regarded this BOM tag as output, so an error occurred!
So the BOM tag must be deleted on 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 Signature", set it to: "Always remove signature",
then save the file , that is, the BOM label can be removed!
4. UTF-8 encoding problem when PHP saves files as attachments:
PHP saves files as attachments, and the file name must be GB2312 encoded.
Otherwise, if there is Chinese in the file name, It will display garbled characters:
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");
Use the program to example the character interception method
Copy the code The code is as follows:

function utf8_substr($str,$len)
{
for($i=0;$i<$len;$i++)
 {
$temp_str=substr($str,0, 1);
if(ord($temp_str) > 127){
$i++;
if($i<$len){
$new_str[]=substr($str,0 ,3);
  $str=substr($str,3);
   }
  }else {
  $new_str[]=substr($str,0,1); str=substr($str,1);
  }
 }
return join($new_str);
}

The problem with MYSQL database using UTF-8 encoding

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:

Copy code The code is as follows:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

When creating a data table: If this field stores Chinese, you need to set "Organization" to: "utf8_general_ci".
If this field stores English or numbers, the default is fine.
The corresponding SQL statement, for example:

Copy code The code is as follows:
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:

Copy the code The code is as follows:
$connection = mysql_connect($host_name, $host_user, $host_pass);

Add two lines:

Copy code The code is as follows :
mysql_query("set character set 'utf8'");//Read library
mysql_query("set names 'utf8'");//Write library

You can read and write the MYSQL database normally.

The environment used is appserv-win32-2.5.10. When installing this package, the default utf8 encoding is used.
When writing the database connection file, write:

Copy the code The code is as follows:
$conn = mysql_connect(" $host","$user","$password");
mysql_query("SET NAMES 'UTF8'");
mysql_select_db("$database",$conn);

Then when making the page, pay attention to this sentence:

Copy the code The code is as follows:



This way, regardless of the Chinese input into the database, The page still displays normally.
In the DW CS4 version, the utf8 page is also generated by default.
Similarly, if you write the database connection file at the beginning:
Copy the code The code is as follows:

mysql_query( "SET NAMES 'GBK'");

The page should also change accordingly:
Copy the code The code is as follows:



Summary, the last thing is page encoding Unification can easily solve the problem of garbled characters, especially the setting of set names in mysql_query() must be consistent with the page and database encoding statistics.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325350.htmlTechArticlePHP page conversion to UTF-8 encoding problem 1. Add a line at the beginning of the code: Copy the code as follows: header( "Content-Type: text/html;charset=utf-8"); 2. For PHP file encoding issues, click on the editor...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template