博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
更改mysql数据库latin1_swedish_ci为utf8
阅读量:5302 次
发布时间:2019-06-14

本文共 2429 字,大约阅读时间需要 8 分钟。

原文在http://bingu.net/472/latin1_swedish_ci-to-utf8_general_ci/

把下列文件保存为一个.php文件,然后运行
<?php
define('DB_NAME', 'putyourdbnamehere'); // 数据库名
define('DB_USER', 'usernamehere'); // MySQL用户名
define('DB_PASSWORD', 'yourpasswordhere'); // 密码
define('DB_HOST', 'localhost'); // 很大可能你无需修改此项
function UTF8_DB_Converter_DoIt() {
$tables = array();
$tables_with_fields = array();
// Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
// we have to make an a stand-alone/direct connection to the database.
$link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
mysql_select_db(DB_NAME, $link_id);
// Gathering information about tables and all the text/string fields that can be affected
// during the conversion to UTF-8.
$resource = mysql_query("SHOW TABLES", $link_id);
while ( $result = mysql_fetch_row($resource) )
$tables[] = $result[0];
if ( !empty($tables) ) {
foreach ( (array) $tables as $table ) {
$resource = mysql_query("EXPLAIN $table", $link_id);
while ( $result = mysql_fetch_assoc($resource) ) {
if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
$tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " . ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
}
}
// Change all text/string fields of the tables to their corresponding binary text/string representations.
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
// Change database and tables to UTF-8 Character set.
mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
// Return all binary text/string fields previously changed to their original representations.
foreach ( (array) $tables_with_fields as $table => $fields ) {
foreach ( (array) $fields as $field_type => $field_options ) {
mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
}
}
// Optimize tables and finally close the mysql link.
foreach ( (array) $tables as $table )
mysql_query("OPTIMIZE TABLE $table", $link_id);
mysql_close($link_id);
} else {
die('<strong>There are no tables?</strong>');
}
return true;
}
UTF8_DB_Converter_DoIt();
?>

转载于:https://www.cnblogs.com/hssbsw/p/3448246.html

你可能感兴趣的文章
js千分位处理
查看>>
js常用的方法
查看>>
Mac---------三指拖移
查看>>
关于VMare中安装Ubuntu的一些说明
查看>>
七、K3 WISE 开发插件《工业单据老单插件中获取登陆用户名》
查看>>
字符串类型的相互转换
查看>>
图片编辑的利器(介绍韩国免费图片工具PhotoScape)
查看>>
Python基础第十一天:递归函数
查看>>
钉钉机器人
查看>>
博雅PHP高级工程师面试题-自拟
查看>>
SQL SERVER 查看表是否存在
查看>>
关于easyUI实现自定义网格视图
查看>>
JAVA小知识点-Finally和Return的执行关系
查看>>
基站转经纬度
查看>>
构建ASP.NET网站十大必备工具
查看>>
a*寻路分析
查看>>
Android Activity的任务栈和四大启动模式
查看>>
table左边固定-底部横向滚动条-demo
查看>>
MySQL事件异常记录
查看>>
Redis 发布订阅
查看>>