Summary of code conversion method for MySQL database

Source: Internet
Author: User
Tags character set create database

Method One

The problem of using UTF-8 encoding in MySQL database

1. When creating databases and datasheets with phpMyAdmin, set the collation to: "Utf8_general_ci" or execute the statement:

CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

When you create a datasheet: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci", if the field is stored in English or numbers, the default is OK.

The corresponding SQL statement, for example:

The code is as follows Copy Code

CREATE TABLE ' Test ' (
' ID ' INT not NULL,
' Name ' VARCHAR CHARACTER SET UTF8 COLLATE utf8_general_ci not NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM;

2. Read and write database in PHP

After connecting to the database:

The code is as follows Copy Code

$connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:

The code is as follows Copy Code

mysql_query ("Set character set ' UTF8 '");/Read Library
mysql_query ("Set names ' UTF8 ')"//write Library

You can read and write the MySQL database normally.


Another way


Step one command line execution: mysqldump--opt-hlocalhost-uroot-p***--default-character-set=lantin1 dbname >/usr/local/dbname.sql

Step two changes the charset=latin1 of the CREATE TABLE statement in the Dbname.sql file to Charset=utf8

Step three adds a "Set names UTF8" before the INSERT statement in the Dbname.sql file.

Step four dbname.sql transcoding to utf-8 format, we recommend that you use Ultraeditor, you can use the editor's convert->ascii to UTF-8 (Unicode edit) directly, or save the file as a UTF-8 (no BOM) format

Step five command line execution: mysql-hlocalhost-uroot-p***--default-character-set=utf8 new_dbname </usr/local/dbname.sql

Related Article

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.