mysql 数据库gbk转utf8 乱码_mysql表utf8编码,内容gbk编码的乱码转换

今天遇到一个数据库库是utf8,表也是utf8,但内容是gbk的问题,出现了中文乱码

第一步  先把所有的表copy一份,比如表名叫 user,copy一份光表结构的,叫user2;

第二步  把表中的数据转码后插入表2的表中

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", "
", $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");

第三步,把原表删除,把2的表改名  ok

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页