标签:
今天遇到一个数据库库是utf8,表也是utf8,但内容是gbk的问题,出现了中文乱码
第一步 先把所有的表copy一份,比如表名叫 user,copy一份光表结构的,叫user2;
第二步 把表中的数据转码后插入表2的表中
<?php header("Content-type: text/html; charset=utf-8"); function get_sql($tablename){ $con = mysql_connect("localhost","root","root"); if (!$con) die(mysql_error()); mysql_select_db("bfwl",$con); mysql_query("set names ‘utf-8‘"); $result = mysql_query("select * from ".$tablename." "); $txt = ""; while($row = mysql_fetch_array($result)){ $sql = ""; foreach($row as $k=>$v){ if(!is_numeric($k)){ $v = iconv("GBK", "UTF-8", $v); $v = str_replace("\r\n", "<br>", $v); $v = trim($v); $v = str_replace("‘", "“", $v); $v = str_replace(‘"‘, "”", $v); $sql .= "$k=‘$v‘,"; } } $sql = trim($sql,","); $sql = "INSERT INTO ".$tablename."2 SET ".$sql.";\r\n"; excute($sql); $txt .= $sql; } //file_put_contents("sql/".$tablename.".sql", $txt); } function excute($sql){ $con = mysql_connect("localhost","root","root"); if (!$con) die(mysql_error()); mysql_select_db("bfwl",$con); mysql_query("set names utf8"); mysql_query($sql); mysql_close($con); } //每个表来一次 get_sql("bingzhongbiao"); get_sql("chat_record"); get_sql("juesebiao"); get_sql("meitibiao"); get_sql("net_chz"); get_sql("net_xf_biao"); get_sql("yishengbiao"); get_sql("yiyuanbiao"); get_sql("yonghubiao"); get_sql("zaixian"); get_sql("zhanzhang");
标签:
原文地址:http://my.oschina.net/tongjh/blog/491848