通过构建SQL语句实现数据同步

6 篇文章 0 订阅
  1. <?php
  2. /**
  3.  * @name mysqlbak.php
  4.  * @author caleng
  5.  * @since 2008-8-1
  6.  * @deprecated 实现数据库数据同步
  7.  * @version 1.0
  8.  */
  9. /**
  10.  * @name connectMysqlServer
  11.  * @param string $pHost,$pUser,$pPwd,$pDbName
  12.  * @return $conn
  13.  * @deprecated connect the mysql db
  14.  */
  15. function connectMySqlServer($pHost$pUser$pPwd$pDbName)
  16. {
  17.     $conn = mysql_connect($pHost$pUser$pPwdor die('mysql connect false:'.mysql_error());
  18.     mysql_select_db($pDbName$conn);
  19.     return $conn;
  20. }
  21. /**
  22.  * @name getMaxTableId
  23.  * @param array $pDB = array('host'=>,'user'=>,'pwd'=>,'name'=>,'table'=>,'id'=>'id')
  24.  * @return int $id
  25.  * @deprecated get the max id value from the $pTable
  26.  */
  27. function getMaxTableId($pDB)
  28. {
  29.     $conn = connectMySqlServer($pDB['host'], $pDB['user'], $pDB['pwd'], $pDB['name']);
  30.     $sql = "select {$pDB['id']} from {$pDB['table']} order by {$pDB['id']} DESC";
  31.     $result = mysql_query($sql$connor die(mysql_error());
  32.     return mysql_result($result, 0, $pDB['id']);
  33.     mysql_close($conn);
  34.     $conn = null;
  35. }
  36. $one = array('host'=>'localhost','user'=>'root','pwd'=>'root','name'=>'china186_main','table'=>'comm_help','id'=>'id');
  37. $tow = array('host'=>'localhost','user'=>'root','pwd'=>'root','name'=>'china186_index','table'=>'comm_help','id'=>'id');
  38. $oneId = getMaxTableId($one);
  39. $towId = getMaxTableId($tow);
  40. if ($oneId > $towId) {
  41.     $reId = $oneId - $towId;
  42.     
  43.     //获取源库中不同的数据
  44.     $sql = "select * from comm_help limit $towId,$reId";
  45.     $conn = connectMySqlServer('localhost','root','root','china186_main');
  46.     mysql_query("SET NAMES UTF8");
  47.     $result = mysql_query($sql$conn);
  48.     if ($result) {
  49.         $reArr = array();
  50.         $i = 0;
  51.         while ($row = mysql_fetch_assoc($result)) {
  52.             $reArr[$i++] = $row;
  53.         }//while
  54.     }//if
  55.     mysql_close($conn);
  56.     $conn = null;
  57.     
  58.     //插入到新数据库中
  59.     $field = '';
  60.     $conn  = connectMySqlServer('localhost','root','root','china186_index');
  61.     for ($i = 0; $i < count($reArr); $i++) {
  62.         $key = implode(',',array_keys($reArr[$i]));
  63.         $value = implode("','",$reArr[$i]);
  64.         $sql = "insert into comm_help (host,$key) values ('mc2008.china186.com','{$value}')";
  65.         mysql_query("SET NAMES UTF8");
  66.         mysql_query($sql,$connor die(mysql_error());
  67.     }
  68.     mysql_close($conn);
  69.     $conn = null;
  70. }
  71. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值