Solution to the problem of Chinese garbled characters written to mysql by php_PHP tutorial

WBOY
Release: 2016-07-13 10:43:03
Original
984 people have browsed it

Connecting to the database in PHP is very simple, but if we do not unify the page and database encoding when inserting and saving data into the MySQL database, the Chinese garbled problem may occur. Let me introduce to you how to prevent this problem.

The operation of mysql in php is very simple. However, when I was making a small tool recently, I found that garbled Chinese characters appeared in the database written by php to mysql. This garbled problem has troubled me for a long time. It is clear that English characters are written It's normal, but the Chinese is garbled!

At first I felt that I did not set the UTF-8 character set when I created the mysql database. However, later I found that even if I set this character set, the problem still could not be solved, and the string written to mysql was still garbled.

Solving this problem is actually very simple.

1. When creating the table, set the encoding type to gb2312_chinese_ci.

2. Add a line to the database connection statement on the PHP page mysql_query("SET NAMES 'gb2312'",$link); For example

$result=mysql_query($query);
The code is as follows
 代码如下 复制代码

$db_host="localhost";
$db_user="root";
$db_password="password";
$db_name="test";

$link=mysql_connect($db_host,$db_user,$db_password);
mysql_query("SET NAMES 'gb2312'",$link);
$db=mysql_select_db($db_name,$link);
$query="select * from user";
$result=mysql_query($query);

Copy code

$db_host="localhost";
$db_user="root"; $db_password="password"; $db_name="test";

 代码如下 复制代码

[mysqld]

default-character-set=utf8

$link=mysql_connect($db_host,$db_user,$db_password);

mysql_query("SET NAMES 'gb2312'",$link);


$db=mysql_select_db($db_name,$link);

$query="select * from user";

Add this line to both the writing page and the reading page. In this way, Chinese characters in MYSQL can be displayed normally. Another way to modify the configuration file my.cnf
The code is as follows Copy code
[mysqld] default-character-set=utf8
Restart MYSQL and change it. This change is to set character_set_server to utf8 Well, these problems about php writing Chinese garbled characters into mysql have been solved. Of course, we mainly pay attention to the encoding between the page and the database and then use mysql_query() to set it. http://www.bkjia.com/PHPjc/633204.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633204.htmlTechArticlePHP connecting to the database is very simple, but when we insert and save data into the mysql database, it may happen if we do not unify the page and database coding. There is a problem with Chinese garbled characters. Let me introduce to you the prediction...
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