Several solutions to php and mysql for uft 8 Chinese coding chaos

  • 2020-05-16 06:33:26
  • OfStack

PHP page to UTF-8 encoding problem
1. Add 1 line at the beginning of the code:
 
header("Content-Type: text/html;charset=utf-8"); 

2.PHP file encoding problem
Click on the editor's menu: "file" - > "Save as", you can see the current file code, make sure the file code is UTF-8,
If it's ANSI, you need to change the code to: UTF-8.
3.PHP header BOM question:
PHP file 1 must not have BOM tags
Otherwise, session will not work, with a similar tip:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
This is because when session_start() is executed, the entire page cannot have output, but when the BOM tag is present on the previous PHP page,
PHP mistook the BOM tag for output, so there was an error!
So PHP page 1 must remove the BOM tag
Delete this BOM tag method:
1. You can open the file with Dreamweaver and save it again, which means you can remove the BOM tag!
2. You can open the file with EditPlus and go to "preferences" - in the menu > "File" > "UTF-8 logo", set to "always delete signature",
Then save the file so you can remove the BOM tag!
4. When PHP saves files as attachments, UTF-8 encoding problems:
PHP saves the file as an attachment, the file name must be GB2312,
Otherwise, if there is Chinese in the file name, it will display garbled code:
If your PHP itself is an UTF-8 encoded file,
The file name variable needs to be changed from UTF-8 to GB2312:
iconv("UTF-8", "GB2312", "$filename");
Use the program to instantiate the character interception method
 
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 MYSQL database USES the UTF-8 encoding problem

1. Create databases and tables with phpmyadmin
When creating the database, set "collation" to "utf8_general_ci"
Or execute statement:
 
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 

When creating the data table: if this field is in Chinese, "collation" needs to be set to "utf8_general_ci",
If the field is in English or Numbers, the default is ok.
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 to the database using 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 the library  
mysql_query("set names 'utf8'");// Write a library  

Can read and write MYSQL database normally.

The appserv-win32-2.5.10 environment is used, and the default utf8 code is used when loading this package.
When writing a database connection file, write:
 
$conn = mysql_connect("$host","$user","$password"); 
mysql_query("SET NAMES 'UTF8'"); 
mysql_select_db("$database",$conn); 

Then, as you work on the page, pay attention to this sentence:
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

So no matter input database Chinese, or page display, will be normal.
In DW CS4, the utf8 page is also generated by default.
Similarly, if 1 starts writing the database connection file as:
 
mysql_query("SET NAMES 'GBK'"); 

The page should also be:
 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 

To sum up, it is very convenient to solve the problem of scrambled code if the page encoding is unified with 1, especially in mysql_query(), the setting of set names must be set together with the page and database encoding statistics 1.

Related articles: