PHP write MySQL Chinese garbled problem resolution _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.