自从第一次将那个.net的导医系统改版为php,并将其数据也导入到新系统后,我这次是第三次做类似的数据转移的东西了,第二次是集团网站,也是.net的,不过是sqlsever的数据库,改版为帝国php+mysql,同样的要将数据库导过来,并且网站功能,文章路径等等,都保证和原来不变,就是这个www.rjtnb.com这个网站,第二个难度是最大的,这次的这个是网站改版,但是因为原来文章收录比较多,所以要导入原来的数据,并且保持文章路径不变。这次就是数据量比较大,6w多文章,但是难度相比前两次要简单。
<?php
exit;
set_time_limit(0);
$dbHost = 'localhost';//服务器
$dbUser = 'root';//用户名
$dbPwd = '123456';//密码
$con = mysql_connect($dbHost,$dbUser,$dbPwd);
if(empty($con)){
echo '连接服务器失败';
} (PS:T不错的Q扣峮,有兴趣可以加入一起讨论:php扣峮:304224365,验证:csl)
mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary",$con);
$conn = mysql_select_db('tp',$con);
if(empty($conn)){
echo '选择数据库失败';
}
$classid = array(
'8' => '33',
'16' => '34',
'19' => '35',
'22' => '36',
'27' => '37',
'42' => '38',
'58' => '39',
'73' => '40',
'81' => '41',
'82' => '42',
'85' => '43',
'86' => '44',
'87' => '45',
'235' => '46',
'240' => '47',
'244' => '48',
'250' => '49',
'253' => '50',
);
$classpath = array(
'8' => '/aboutus/article/',
'16' => '/news/article/',
'19' => '/class/article/',
'22' => '/dept/article/',
'27' => '/bfz/article/',
'42' => '/tnbxt/article/',
'58' => '/jkcs/article/',
'73' => '/tslf/article/',
'81' => '/ljtnb/article/',
'82' => '/xjsb/article/',
'85' => '/xtjc/article/',
'86' => '/tnbclub/article/',
'87' => '/jzzn/article/',
'235' => '/tnblx/article/',
'240' => '/2011/article/',
'244' => '/taiwan/article/',
'250' => '/school/article/',
'253' => '/kfbl/article/',
);
$sql = "select `id`,`title`,`content`,`addtime`,`updatetime`,`key`,`intro`,`classid` from `cms_article` where 1 order by id asc";
$query = mysql_query($sql);
while($rs = mysql_fetch_array($query)){
//dump($rs);
$cid = $classid[$rs['classid']];
$data1 = array(
'id' =>$rs['id'],
'classid' =>$cid,
'ttid' =>0,
'onclick' => 1,
'plnum' => 0,
'totaldown' => 0,
'filename' => $rs['id'],
'userid' =>1,
'username' => 'manage',
'firsttitle' => 0,
'isgood' => 0,
'ispic' => 0,
'istop' => 0,
'isqf' => 0,
'ismember' => 0,
'truetime' => $rs['addtime'],
'lastdotime' => $rs['updatetime'],
'havehtml' => 0,
'groupid' => 0,
'userfen' => 0,
'titleurl' => $classpath[$rs['classid']].$rs['id'].'.html',
'stb' => 1,
'fstb' => 1,
'restb' => 1,
'keyboard' => $rs['key'],
'title' => $rs['title'],
'newstime' => $rs['addtime'],
'smalltext' => $rs['intro'],
'diggtop' => 0,
);
insert('phome_ecms_news_old',$data1);
$data2=array(
'id' =>$rs['id'],
'classid' =>$cid,
'dokey' =>1,
'newstempid' => 0,
'closepl' => 0,
'haveaddfen' => 0,
'newstext' => $rs['content'],
);
insert('phome_ecms_news_old_data_1',$data2);
$data3=array(
'id' =>$rs['id'],
'classid' =>$cid,
'checked' =>1,
'newstime' => $rs['addtime'],
'truetime' => $rs['addtime'],
'lastdotime' => $rs['updatetime'],
'havehtml' => 0,
);
insert('phome_ecms_news_old_index',$data3);
}
function dump($s){
echo '<pre>';
var_dump($s);
echo '</pre>';
}
function insert($table,$data=array()){
$keys=array_keys($data);
$z = "`".implode("`,`",$keys)."`";
$v = "'".implode("','",$data)."'";
$sql = "insert into `$table` ($z) values($v)";
mysql_query($sql);
}
上面是代码,这个是帝国7.0的,只是让大家参考一下,其实原理很简单,不过就是要对自己的系统非常熟悉,数据表的字段结构等等,然后就是要分析目标数据库,其实并没有什么太难的。对了,前端时间,我们的一个网站不知道什么原因,从某个日期以后的数据全部没有了,但是页面还在,也就是数据库出了问题,但是帝国生成静态文件,所以还能访问到,我就利用采集,采集自己的文章,赛选出那个日期以后的,并且根据规则,让其路径和原来文章已有的路径保持不变,但是id肯定是变了,帝国的采集是这样的,他会想将信息和每个文章的目标网址保存在一个临时表里,然后我们采集完,留下要导入的信息,然后从数据表利用sql语句替换他的一个用不到但是要导入系统的字段为目标网址(比如ftitle副标题等),然后导入,然后我们可以根据这个字段,用sql或者程序修改。