MySQL 5 solves the problem of garbled data writing and reading

Source: Internet
Author: User
Example of how to solve mysql5's write and read garbled characters

The code is as follows:

Require ("adodb/adodb. inc. php ");
$ Conn = newadoconnection ('mysql ');
$ Conn-> connect ("localhost", "root", "2027205", "bh38") or die ("connection failed ");
$ Conn-> execute ("set names gb2312 ");
$ Conn-> execute ("insert into 'vv '('CC') VALUES (' I don't know if I have changed the encoding ');") or die ("error ");
$ Rc = $ conn-> execute ("select * from vv ");
While (! $ Rc-> EOF)
{
Echo ($ rc-> fields ["cc"]);
$ Rc-> movenext ();
}
?>


Of course, we can also use the following command to modify the character set of the database:
Alter database da_name default character set 'charset '.
The client sends data in gbk format. the following configurations can be used:
SET character_set_client = 'gbk'
SET character_set_connection = 'gbk'
SET character_set_results = 'gbk'
This configuration is equivalent to set names 'gbk '.
Perform operations on the database you just created
Mysql> use test;
Database changed
Mysql> insert into mysqlcode values (null, 'php hobby ');
ERROR 1406 (22001): Data too long for column 'content' at row 1
If the character set is not specified as gbk, an error occurs during insertion.
Mysql> set names 'gbk ';
Query OK, 0 rows affected (0.02 sec)
The specified character set is gbk.
Mysql> insert into mysqlcode values (null, 'php hobby ');
Query OK, 1 row affected (0.00 sec)
Inserted successfully
Mysql> select * from mysqlcode;
+ ---- + ----------- +
| Id | content |
+ ---- + ----------- +
| 1 | php hobbies |
+ ---- + ----------- +
1 row in set (0.00 sec)
Garbled characters are also displayed when the character set gbk is not specified.
Mysql> select * from mysqlcode;
+ ---- + --------- +
| Id | content |
+ ---- + --------- +
| 1 | php ??? |
+ ---- + --------- +
1 row in set (0.00 sec)

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.