不会的要多查多问,不然不会的永远不会,哪怕你离会就差了那么一点点
ucenter数据合并转换
[ 2010/09/02 22:52 | by 刘新修 ]
实现两个ucenter之间的数据合并。合并的时候发现目标ucenter已经存在帐号就不再导入。源数据库为gbk 目标数据库为utf-8
PHP代码
- <?php
- $old_link=mysql_connect("192.168.22.30","root","") or die("connect old db fail");
- mysql_select_db("gbk", $old_link) or die("select old db fail");
- mysql_query("SET NAMES 'GBK'",$old_link);
- $new_link=mysql_connect("192.168.79.42","root","root") or die("connect new db fail");
- mysql_select_db("beijing_bbs", $new_link) or die("select new db fail");
- mysql_query("SET NAMES 'UTF-8'",$new_link);
- $old_uc_dbname="hcdb_members";
- $new_uc_dbname="uc_members";
- $maxuid=file_get_contents("old2new.txt");
- $old_sql="select * from $old_uc_dbname where uid>$maxuid limit 1";
- $old_result=mysql_query($old_sql,$old_link);
- while($old_row=mysql_fetch_array($old_result)){
- $uid=$old_row["uid"];
- $username= iconv('GBK','UTF-8',$old_row["username"]);
- $password=$old_row["password"];
- $email=$old_row["email"];
- $myid=$old_row["myid"];
- $myidkey=$old_row["myidkey"];
- $regip=$old_row["regip"];
- $regdate=$old_row["regdate"];
- $lastloginip=$old_row["lastloginip"];
- $lastlogintime=$old_row["lastlogintime"];
- $salt=$old_row["salt"];
- $secques=$old_row["secques"];
- $sql="select uid from $new_uc_dbname where username='$username'";
- mysql_query($sql,$new_link);
- $num=mysql_affected_rows($new_link);
- echo $num.$username."<br>";
- if($num==0){
- $sql="insert into $new_uc_dbname(username,password,email,myid,myidkey,regip,regdate,lastloginip,lastlogintime,salt,secques) values('$username','$password','$email','$myid','$myidkey','$regip','$regdate','$lastloginip','$lastlogintime','$salt','$secques')";
- echo "$sql";
- mysql_query($sql,$new_link);
- echo "${username}插入成功<br>";
- }else
- {
- echo "${username}已经存在<br>\n";
- }
- file_put_contents("old2new.txt",$uid);
- }
- ?>