Home > Article > Backend Development > How to solve the problem of Chinese garbled characters written to the database by PHP
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.
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");
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");
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); }
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;
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 ;
2. Use PHP to read and write the database
After connecting to the database:
$connection = mysql_connect($host_name, $host_user, $host_pass);
Add two lines:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库 //其实读写都可以只加入 mysql_query("set names 'utf8'");
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 'UTF8'"); mysql_select_db("$database",$conn);
Then when making the page, pay attention to this sentence:
073663b537ec255bed1856383582f0d7
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" />
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!