通过构建SQL语句实现数据同步
2008-09-19 16:07
441 查看
<?php
/**
* @name mysqlbak.php
* @author caleng
* @since 2008-8-1
* @deprecated 实现数据库数据同步
* @version 1.0
*/
/**
* @name connectMysqlServer
* @param string $pHost,$pUser,$pPwd,$pDbName
* @return $conn
* @deprecated connect the mysql db
*/
function connectMySqlServer($pHost, $pUser, $pPwd, $pDbName)
{
$conn = mysql_connect($pHost, $pUser, $pPwd) or die('mysql connect false:'.mysql_error());
mysql_select_db($pDbName, $conn);
return $conn;
}
/**
* @name getMaxTableId
* @param array $pDB = array('host'=>,'user'=>,'pwd'=>,'name'=>,'table'=>,'id'=>'id')
* @return int $id
* @deprecated get the max id value from the $pTable
*/
function getMaxTableId($pDB)
{
$conn = connectMySqlServer($pDB['host'], $pDB['user'], $pDB['pwd'], $pDB['name']);
$sql = "select {$pDB['id']} from {$pDB['table']} order by {$pDB['id']} DESC";
$result = mysql_query($sql, $conn) or die(mysql_error());
return mysql_result($result, 0, $pDB['id']);
mysql_close($conn);
$conn = null;
}
$one = array('host'=>'localhost','user'=>'root','pwd'=>'root','name'=>'china186_main','table'=>'comm_help','id'=>'id');
$tow = array('host'=>'localhost','user'=>'root','pwd'=>'root','name'=>'china186_index','table'=>'comm_help','id'=>'id');
$oneId = getMaxTableId($one);
$towId = getMaxTableId($tow);
if ($oneId > $towId) {
$reId = $oneId - $towId;
//获取源库中不同的数据
$sql = "select * from comm_help limit $towId,$reId";
$conn = connectMySqlServer('localhost','root','root','china186_main');
mysql_query("SET NAMES UTF8");
$result = mysql_query($sql, $conn);
if ($result) {
$reArr = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$reArr[$i++] = $row;
}//while
}//if
mysql_close($conn);
$conn = null;
//插入到新数据库中
$field = '';
$conn = connectMySqlServer('localhost','root','root','china186_index');
for ($i = 0; $i < count($reArr); $i++) {
$key = implode(',',array_keys($reArr[$i]));
$value = implode("','",$reArr[$i]);
$sql = "insert into comm_help (host,$key) values ('mc2008.china186.com','{$value}')";
mysql_query("SET NAMES UTF8");
mysql_query($sql,$conn) or die(mysql_error());
}
mysql_close($conn);
$conn = null;
}
?>
/**
* @name mysqlbak.php
* @author caleng
* @since 2008-8-1
* @deprecated 实现数据库数据同步
* @version 1.0
*/
/**
* @name connectMysqlServer
* @param string $pHost,$pUser,$pPwd,$pDbName
* @return $conn
* @deprecated connect the mysql db
*/
function connectMySqlServer($pHost, $pUser, $pPwd, $pDbName)
{
$conn = mysql_connect($pHost, $pUser, $pPwd) or die('mysql connect false:'.mysql_error());
mysql_select_db($pDbName, $conn);
return $conn;
}
/**
* @name getMaxTableId
* @param array $pDB = array('host'=>,'user'=>,'pwd'=>,'name'=>,'table'=>,'id'=>'id')
* @return int $id
* @deprecated get the max id value from the $pTable
*/
function getMaxTableId($pDB)
{
$conn = connectMySqlServer($pDB['host'], $pDB['user'], $pDB['pwd'], $pDB['name']);
$sql = "select {$pDB['id']} from {$pDB['table']} order by {$pDB['id']} DESC";
$result = mysql_query($sql, $conn) or die(mysql_error());
return mysql_result($result, 0, $pDB['id']);
mysql_close($conn);
$conn = null;
}
$one = array('host'=>'localhost','user'=>'root','pwd'=>'root','name'=>'china186_main','table'=>'comm_help','id'=>'id');
$tow = array('host'=>'localhost','user'=>'root','pwd'=>'root','name'=>'china186_index','table'=>'comm_help','id'=>'id');
$oneId = getMaxTableId($one);
$towId = getMaxTableId($tow);
if ($oneId > $towId) {
$reId = $oneId - $towId;
//获取源库中不同的数据
$sql = "select * from comm_help limit $towId,$reId";
$conn = connectMySqlServer('localhost','root','root','china186_main');
mysql_query("SET NAMES UTF8");
$result = mysql_query($sql, $conn);
if ($result) {
$reArr = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$reArr[$i++] = $row;
}//while
}//if
mysql_close($conn);
$conn = null;
//插入到新数据库中
$field = '';
$conn = connectMySqlServer('localhost','root','root','china186_index');
for ($i = 0; $i < count($reArr); $i++) {
$key = implode(',',array_keys($reArr[$i]));
$value = implode("','",$reArr[$i]);
$sql = "insert into comm_help (host,$key) values ('mc2008.china186.com','{$value}')";
mysql_query("SET NAMES UTF8");
mysql_query($sql,$conn) or die(mysql_error());
}
mysql_close($conn);
$conn = null;
}
?>
相关文章推荐
- excel数据通过构建sql语句导入到数据库中
- C# 执行多条SQL语句,实现数据库事务(通过Hashtable存储数据) .
- 通过抽象Sql语句来构建流畅的数据访问
- C# 执行多条SQL语句,实现数据库事务(通过Hashtable存储数据) .
- sql server 2000与sql server ce2.0通过SqlCeRemoteDataAccess实现数据同步
- 表级数据同步:通过merge,database links,procedure,job实现
- MySql的sql语句中添加存储过程或者存储函数来实现Oracle中的start with ……connect by prior……递归(树形结构数据)查询
- 通过T-SQL语句实现数据库备份与还原的代码
- Sql 语句实现在同一个DB中复制表和空清一个DataTable中的所有数据
- ADO实现与SQL的数据同步
- SQL语句-实现对数据中两张表的访问
- 通过SQL语句转换WordPress数据
- sparkSQL里 sql语句,dataframe,Thrift Server JDBC都可以实现对数据的查询,过滤等操作, 哪这3种情况分别是什么情况下使用
- JAVA通过Gearman实现MySQL到Redis的数据同步(异步复制)
- 一条SQL语句实现:一行多个字段数据的最大值。
- 在SqlServer中通过SQL语句实现树状查询
- 【转】在SQL Server中通过SQL语句实现分页查询
- mysql使用sql语句同步数据
- SQL语句实现合并数据(原创)
- 使用Phoenix通过sql语句更新操作hbase数据