如何将数据库中的GB2312编码转换成utf-8编码

来源:互联网 发布:数据建模分析 工具 编辑:程序博客网 时间:2024/05/02 07:55
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>


<body>
<?php
$dbhost="localhost";
$dbuser="root";
$dbpassword = "";


$dbname="2012";
$icen_news_archives1="icen_news_archives1";
 mysql_connect($dbhost,$dbuser,$dbpassword) or die("数据库出了点小问题,对不起!请稍后访问......");
 mysql_select_db($dbname) or die("数据库现在繁忙,请稍后访问......");
 
   $sql="select * from $icen_news_archives1";
  $query=mysql_query($sql);
  /*----------------------先把数据库中的原来的数据读出来--------------------------*/
  while($records=mysql_fetch_array($query, MYSQL_ASSOC))
  {
 $cid=$records['cid'];
 $authorName=$records['authorName'];
 $nTime=$records['nTime'];
 $nCount=$records['nCount'];
 $nTitle=$records['nTitle'];
 $isTitleBold=$records['isTitleBold'];
      
 $cid=iconv("GB2312", "UTF-8", "$cid");   //  转换编码
 $authorName=iconv("GB2312", "UTF-8", "$authorName");
 $nTime=iconv("GB2312", "UTF-8", "$nTime");
 $nCount=iconv("GB2312", "UTF-8", "$nCount");
 $nTitle=iconv("GB2312", "UTF-8", "$nTitle");
 $isTitleBold=iconv("GB2312", "UTF-8", "$isTitleBold");
     
 mysql_query("set names utf8");//转换字符集
  $sql1="insert into icen_news_archives (nid,cid,authorName,nTime,nCount,nTitle,isTitleBold)values(NULL, '$cid', '$authorName', '$nTime', '$nCount', '$nTitle', '$isTitleBold')";  // 把转换后的结果一条条插入新库,这样也会防止数据丢失
$query1=mysql_query($sql1);
if($query1)
 {
 echo "1";
}else{ 
     echo "0"; 
}
  }
?>
</body>
</html>
原创粉丝点击