首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  MYSQL/SQL语句/PHPMYADMIN专栏 » [转帖]小谈MYSQL字符集设置  


  作者:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

  nqp@nqp.me
  4304410
  www.nqp.me

 

 发表:2006-03-02 17:23:18 阅读 4427 次 回复 2 次 得分2  |   字号 字色
[转帖]小谈MYSQL字符集设置
首先,这片文章纯粹是我的个人经验之谈,适用于我常见的环境及项目中.
个人建议,数据库字符集尽量使用utf8(utf-8),以使你的数据能很顺利的实现迁移,因为utf8字符集是目前最适合于实现多种不同字符集之间的转换的字符集,尽管你在命令行工具上无法正确查看数据库中的内容,我依然强烈建议使用utf8作为默认字符集.
接下来是完整的一个例子:
1.创建数据库表
mysql>CREATE DATABASE IF NOT EXISTS my_db default charset utf8 COLLATE utf8_general_ci;
#注意后面这句话 "COLLATE utf8_general_ci",大致意思是在排序时根据utf8变码格式来排序
#那么在这个数据库下创建的所有数据表的默认字符集都会是utf8了

mysql>create table my_table (name varchar(20) not null default '')type=myisam default charset utf8;
#这句话就是创建一个表了,制定默认字符集为utf8

2.写数据
例子1是通过php直接插入数据:
a.php
<?php
mysql_connect('localhost','user','password');
mysql_select_db('my_db');

//请注意,这步很关键,如果没有这步,所有的数据读写都会不正确的
//它的作用是设置本次数据库联接过程中,数据传输的默认字符集
mysql_query("set names utf8;");

//必须将gb2312(本地编码)转换成utf-8,也可以使用iconv()函数
mysql_query(mb_convet_encoding("insert into my_table values('测试');", "utf-8", "gb2312"));
?>

例子是通过页面提交插入数据2:
b.php
<?php
//输出本页编码为utf-8
header("content-type:text/html; charset=utf-8");

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

if(isset($_REQUEST['name'))
{
    //由于上面已经指定本页字符集为utf-8了,因此无需转换编码
    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="" method="post">
<input type="text" name="name" value="">
<input type="submit" value='submit'>
</form>

自此,使用utf8字符集的完整的例子结束了.
如果你想使用gb2312编码,那么建议你使用latin1作为数据表的默认字符集,这样就能直接用中文在命令行工具中插入数据,并且可以直接显示出来.而不要使用gb2312或者gbk等字符集,如果担心查询排序等问题,可以使用binary属性约束,例如:
create table my_table ( name varchar(20) binary not null default '')type=myisam default charset latin1;


附:旧数据升级办法
以原来的字符集为latin1为例,升级成为utf8的字符集。原来的表: old_table (default charset=latin1),新表:new_table(default charset=utf8)。
第一步:导出旧数据
mysqldump --default-character-set=latin1 -hlocalhost -uroot -B my_db --tables old_table > old.sql
第二步:转换编码
iconv -t utf-8 -f gb2312 -c old.sql > new.sql
在这里,假定原来的数据默认是gb2312编码。
第三步:导入
修改old.sql,增加一条sql语句: "SET NAMES utf8;",保存。
mysql -hlocalhost -uroot my_db < new.sql
大功告成!!

 
 1#楼  
 
  回复人:emily
  注册时间:2006-07-06
  主题/回复:7/11
  积分:446
  等级:★★☆(五级)
  称号:略有小成

用户联系方式已设置为保密
 1#楼 发表于2006-07-06 18:36:35  评分:1 

有几个小问题,,注意下括号就ok
 2#楼  
 
  回复人:nana001
  注册时间:2008-06-13
  主题/回复:0/10
  积分:43
  等级:★(二级)
  称号:初出茅庐

   
 2#楼 发表于2008-06-16 21:46:31  评分:1 

回复给楼主(一起PHP)
[*]MySQL Tutorial ebook free download
[*]MySQL Cookbook ebook free download
[*]MySQL in a Nutshell ebook free download
[*]MySQL and mSQL ebook free download
[*]Expert MySQL ebook free download
[*]MySQL Administrator's Guide ebook free download
[*]Mastering MySQL 4 ebook free download
[*]MySQL Pocket Reference ebook free download
[*]MySQL Crash Course ebook free download
[*]High Performance MySQL ebook free download
[*]MySQL and PHP from Scratch ebook free download
[*]MySQL, Second Edition ebook free download
[*]MySQL: Essential Skills ebook free download
[*]Understanding MySQL Internals ebook free download
[*]PHP5 and MySQL Bible ebook free download
[*]Learning PHP and MySQL ebook free download
[*]MySQL Certification Study Guide ebook free download
[*]MySQL Stored Procedure Programming ebook free download
[*]PHP and MySQL Web Development ebook free download
[*]MySQL Database Design and Tuning ebook free download
[*]MySQL Cookbook, 2nd Edition ebook free download
[*]The Definitive Guide to MySQL, Second Edition ebook free download
[*]MySQL Phrasebook: Essential Code and Commands ebook free download
[*]Managing and Using MySQL, 2nd Edition ebook free download
[*]Sams Teach Yourself MySQL in 10 Minutes ebook free download
[*]MySQL 5.0 Certification Study Guide ebook free download
[*]Web Database Applications with PHP &amp; MySQL ebook free download
[*]Sams Teach Yourself PHP, MySQL and Apache All in One ebook free download
[*]Mysql Pocket Reference SECOND EDITION ebook free download
[*]Mysql Enterprise Solutions Alexander “Sasha” Pachev ebook free download
[*]The Definitive Guide to MySQL 5, Third Edition ebook free download
[*]Learning PHP and MySQL, Second Edition ebook free download
[*]Mastering phpMyAdmin 2.11 for Effective MySQL Management ebook free download
[*]MySQL and Java Developer’s Guide ebook free download
[*]Mysql Administrator's Guide and dLanguage Reference ebook free download
[*]PHP and MySQL For Dummies, 2nd Edition  ebook free download
[*]Database Design Manual: using MySQL for Windows ebook free download
[*]Mastering phpMyAdmin 2.8 for Effective MySQL Management ebook free download
[*]Beginning PHP5, Apache, and MySQL Web Development ebook free download
[*]MySQL The definitive guide to using, programming, and administering MySQL 4.1 and 5.0 Third Edition ebook free download
[*]MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL ebook free download
[*]Build Your Own Database-Driven Website Using PHP &amp; MySQL ebook free download
[*]Sams Teach Yourself PHP, MySQL and Apache in 24 Hours ebook free download
[*]Web Database Applications with PHP &amp; MySQL, 2nd Edition ebook free download
[*]JDBC Metadata, MySQL, and Oracle Recipes: A Problem-Solution Approach ebook free download
[*]Beginning PHP and MySQL 5: From Novice to Professional, Second Edition ebook free download
[*]Practical PHP and MySQL Building Eight Dynamic Web Applications ebook free download
[*]Beginning PHP and MySQL: From Novice to Professional, Third Edition ebook free download
[*]Setting Up LAMP: Getting Linux, Apache, MySQL, and PHP Working Together ebook free download
[*]Creating your MySQL Database: Practical Design Tips and Techniques ebook free download
[*]Professional LAMP: Linux, Apache, MySQL, and PHP5 Web Development ebook free download
[*]Sams Teach Yourself PHP, MySQL And Apache: All in One, Third Edition ebook free download
[*]Intrusion Detection Systems with Snort Advanced IDS Techniques Using Snort, Apache, MySQL, PHP, and ACID ebook free download
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.035sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版