How to solve the problem of Chinese garbled characters written to the database by PHP

藏色散人
Release: 2023-03-12 22:24:01
Original
3807 people have browsed it

Solution to garbled Chinese characters written into the database by PHP: 1. Create a database and set the "utf8_general_ci" encoding; 2. Use PHP to read and write the database, and then set "mysql_query("set names 'utf8'");" That’s it.

How to solve the problem of Chinese garbled characters written to the database by PHP

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to solve php writing The problem of garbled Chinese characters in the database?

The problem of garbled Chinese characters written into the database by PHP

The problem of converting the PHP page to UTF-8 encoding

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

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

2.PHP file encoding problem
Click the menu of the editor: "File"->"Save As", you can see the encoding of the current file, ensure the file encoding Is: UTF-8,
If it is ANSI, you need to change the encoding 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 when the BOM tag exists on the previous PHP page,
PHP regarded this BOM tag as output, so an error occurred!
Therefore, the BOM tag must be deleted on the PHP page
Method to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it, that is, you can 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 signatures",
Then save the file , that is, the BOM label can be removed!
4. 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 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");
Copy after login

Utilization program Let’s take an example of 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); 
}
Copy after login

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 change " Set "Organization" to: "utf8_general_ci" or execute the statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Copy after login

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 ;
Copy after login

2. Use PHP to read and write the database

After connecting to the database:

$connection = mysql_connect($host_name, $host_user, $host_pass);
Copy after login

Add two lines:

mysql_query("set character set &#39;utf8&#39;");//读库 
mysql_query("set names &#39;utf8&#39;");//写库 
//其实读写都可以只加入
mysql_query("set names &#39;utf8&#39;");
Copy after login

You can read and write the MYSQL database normally.

The environment made using appserv-win32-2.5.10 uses the default utf8 encoding when installing this package.
When writing the database connection file, write:

$conn = mysql_connect("$host","$user","$password"); 
mysql_query("SET NAMES &#39;UTF8&#39;"); 
mysql_select_db("$database",$conn);
Copy after login

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


In this way, regardless of the Chinese input into the database or the page display, everything will be normal.
In the DW CS4 version, the utf8 page is also generated by default.
Similarly, if you write the database connection file at the beginning as: mysql_query("SET NAMES 'GBK'");

Then the page should also change accordingly:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the problem of Chinese garbled characters written to the database by PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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