LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 712|回复: 1

在网上找了篇文章,能临时性解决mysql数据库乱码

[复制链接]
发表于 2010-1-31 20:47:18 | 显示全部楼层 |阅读模式
最近做网站,服务器用debian系统,发现后台mysql的数据库乱码,网上找了个暂时性的解决方法
把下列文件保存为一个.php文件,然后运行


  1. <?php
  2. define('DB_NAME', 'putyourdbnamehere');    // 数据库名
  3. define('DB_USER', 'usernamehere');     // MySQL用户名
  4. define('DB_PASSWORD', 'yourpasswordhere'); // 密码
  5. define('DB_HOST', 'localhost');    // 很大可能你无需修改此项

  6. function UTF8_DB_Converter_DoIt() {
  7.         $tables = array();
  8.         $tables_with_fields = array();

  9.         // Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
  10.         // we have to make an a stand-alone/direct connection to the database.
  11.         $link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
  12.         mysql_select_db(DB_NAME, $link_id);

  13.         // Gathering information about tables and all the text/string fields that can be affected
  14.         // during the conversion to UTF-8.
  15.         $resource = mysql_query("SHOW TABLES", $link_id);
  16.         while ( $result = mysql_fetch_row($resource) )
  17.                 $tables[] = $result[0];

  18.         if ( !empty($tables) ) {
  19.                 foreach ( (array) $tables as $table ) {
  20.                         $resource = mysql_query("EXPLAIN $table", $link_id);
  21.                         while ( $result = mysql_fetch_assoc($resource) ) {
  22.                                 if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
  23.                                         $tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " .  ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
  24.                         }
  25.                 }

  26.                 // Change all text/string fields of the tables to their corresponding binary text/string representations.
  27.                 foreach ( (array) $tables as $table )
  28.                         mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);

  29.                 // Change database and tables to UTF-8 Character set.
  30.                 mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
  31.                 foreach ( (array) $tables as $table )
  32.                         mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);

  33.                 // Return all binary text/string fields previously changed to their original representations.
  34.                 foreach ( (array) $tables_with_fields as $table => $fields ) {
  35.                         foreach ( (array) $fields as $field_type => $field_options ) {
  36.                                 mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
  37.                         }
  38.                 }

  39.                 // Optimize tables and finally close the mysql link.
  40.                 foreach ( (array) $tables as $table )
  41.                         mysql_query("OPTIMIZE TABLE $table", $link_id);
  42.                 mysql_close($link_id);
  43.         } else {
  44.                 die('<strong>There are no tables?</strong>');
  45.         }

  46.         return true;
  47. }
  48. UTF8_DB_Converter_DoIt();
  49. ?>
复制代码

为什么说是临时性的呢?因为你用了这个脚本,数据库里原来的数据不乱码了,但是你新加入的数据依然是乱码,正在寻找方法根本性的解决这个问题
发表于 2010-1-31 22:16:53 | 显示全部楼层
全utf8也乱码?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表