PHP connection to the database is very simple, but when we insert the data into the MySQL database, if not unified page and database encoding can appear in Chinese garbled problem, let me introduce you to pre-stop this problem occurs.
PHP in the operation of the MySQL is very simple, but recently in a small tool to find PHP written in MySQL database in the Chinese characters garbled, this garbled problem tangled me for a long time, obviously English characters write normal, but Chinese is garbled!
At first I felt like I didn't set up the UTF-8 character set when I built the MySQL database, but then I found that even though I set up this charset, I couldn't solve the problem, and the string that was written to MySQL was garbled.
It is very simple to solve the problem.
1. Set the encoding type to GB2312_CHINESE_CI when building the table.
2. The database connection statement on the PHP page adds a line mysql_query ("SET NAMES ' gb2312 '", $link); For example
| The code is as follows |
Copy Code |
$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); |
Both the Write page and the Read page are added to this line. So the Chinese in MySQL will show up normally.
Another way to modify the configuration file my.cnf
| The code is as follows |
Copy Code |
[Mysqld] Default-character-set=utf8 |
Restart MySQL This change is to set Character_set_server to UTF8.
Well, to these things about PHP writing MySQL Chinese garbled problem solved, of course, we mainly pay attention to the code between the page and the database and then use mysql_query () to set up.
http://www.bkjia.com/PHPjc/633204.html www.bkjia.com true http://www.bkjia.com/PHPjc/633204.html techarticle PHP Connection database is very simple, but when we insert the data into the MySQL database, if not unified page and database encoding can appear in Chinese garbled problem, let me introduce you to the pre-stop ...