UTF-8 encoding for MySQL Databases

Source: Internet
Author: User

I don't know why the first time I created a table. UTF-8 cannot contain multiple Chinese characters. I used gb2312 instead of creating the table. As a result, the server database encountered a coding problem and had a headache. In this idle time, the table creation experiment proves that UTF-8 can withstand multiple Chinese characters. In this way, you can avoid the problem of unclear character conversion in the future. The method is as follows:

Create a MySQL table:
Create Table t_utf8
(
Id int not null auto_increment,
Title varchar (20) not null,
Contnet varchar (300) defalut null,
Primary Key ('id ')
) Begin = MyISAM default charset = utf8;

Note: When querying the database, set the default Character Set of the database to gb2312. Otherwise, a bunch of question marks will be displayed when you open the database ??.

PHP program:
<? PHP
$ Link = mysql_connect (host, username, PWD)
Mysql_select_db (Database, $ link );
/*
Insert Chinese Characters
Mysql_query ("set names utf8"); // ensure that UTF-8 data is inserted into the database.

$ Re = mysql_query ("insert into t_utf8 (title, content) values ('Chinese characters ', 'Chinese character set Chinese Character Set character Set Chinese Character Set ') ");

*/

Mysql_query ("set names utf8"); // ensure that data is output with UTF-8
$ Re = mysql_query ("select * From t_utf8 ");
While ($ ROW = mysql_fetch_array ($ re ))
{
Echo "". $ row [1]. "<br/>". $ row [2];
}

?>

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.