Linux under MySQL 5.5 Chinese garbled solution

Source: Internet
Author: User
Tags character set database join mysql view

Analysis issues Troubleshooting login MySQL view with show VARIABLES like ' character% ';

The default character set for Character_set_database and Character_set_server is latin1.

+--------------------------+----------------------------+
| variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | UTF8 |
| character_set_connection | UTF8 |
| Character_set_database | Latin1 |
| Character_set_filesystem | binary |
| Character_set_results | UTF8 |
| Character_set_server | Latin1 |
| Character_set_system | UTF8 |
| Character_sets_dir | /usr/share/mysql/charsets/|

Solving method

Modify the character Set key value in the MySQL my.cnf file

1. Add Default-character-set=utf8 to the [client] field as follows:

The code is as follows Copy Code

[Client]
Port = 3306
Socket =/var/lib/mysql/mysql.sock
Default-character-set=utf8

2. Add the Character-set-server=utf8 in the [mysqld] field as follows:

The code is as follows Copy Code

[Mysqld]
Port = 3306
Socket =/var/lib/mysql/mysql.sock
Character-set-server=utf8

3, in the [MySQL] field to add Default-character-set=utf8, as follows:

The code is as follows Copy Code

[MySQL]
No-auto-rehash
Default-character-set=utf8

After the modification is complete, service MySQL Restart restart MySQL services will take effect

Solution II

Modify the character set of each database in the MySQL server

If there is a database before setting the server character set. You need to change the character set of the database:

The code is as follows Copy Code

Use Datatable_name;
ALTER DATABASE character set GBK;

Operating when a program query is written

Example 1 is to insert data directly through PHP:
a.php

The code is as follows Copy Code

<?php
mysql_connect (' localhost ', ' user ', ' password ');
mysql_select_db (' my_db ');

Note that this step is critical, and without this step, all data will be read and written incorrectly.
Its role is to set the default character set for data transmission during this database join process
mysql_query ("Set names UTF8;");

You must convert the gb2312 (local encoding) to Utf-8, or you can use the Iconv () function
mysql_query (mb_convet_encoding ("INSERT into my_table values (' Test ');", "Utf-8", "gb2312"));
?>

Examples are insert data by page Submission 2:
b.php

The code is as follows Copy Code

<?php
//Output This page is encoded as Utf-8
header ("content-type:text/html; Charset=utf-8 ");

mysql_connect (' localhost ', ' user ', ' password ');
mysql_select_db (' my_db ');

If (isset ($_request[' name '))
{
//Because the character set on this page has been specified above, there is no need to convert the encoding
mysql_query sprintf ("INSERT INTO My_ Table values ('%s '); ", $_request[' name '));
}

$q = mysql_query ("SELECT * from My_table"),
while ($r = Mysql_fetch_row ($q))
{
Print_r ($r);
}
?

<meta http-equiv= "Content-type" content= "text/html charset=utf-8"
<form "" action= "POST"
<input type= "text" name= "name" value= ""
<input type= "Submit" value= ' Submit '
</form

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.