<?
/*
* 李海涛 2004-11-5,从外网mysql数据库中将招标编号(子项和包的编号)取到import中oracle的bid_code表里。定时同步(暂定2小时同步一次)
*/
session_start();
include ("include/const.php");
$db_user = "\"PHP\"";
if (!session_is_registered($g_dbh)) {
$Database = new Database;
$Database->ConnectDatabase();
$g_dbh = $Database-> $dbh;
session_register('g_dbh');
}
//连接oracle
$Link = mysql_connect("203.81.21.81", "import_ro", "import");
@ mysql_select_db("release", $Link);
//连接mysql
$sql = "select max(to_char(utime,'YYYY-MM-DD HH24:MI:SS')) from bid_code where utime is not null";
$stmt = ociparse($g_dbh, $sql);
ociexecute($stmt, OCI_DEFAULT);
ocifetchinto($stmt, & $result_array);
$max_ora_bpp_id = $result_array[0];
//获取oracle中bid_code中最大的bpp_id
$sql = "select max(id) from updateRec";
$stmt = ociparse($g_dbh, $sql);
ociexecute($stmt, OCI_DEFAULT);
ocifetchinto($stmt, & $result_array);
$max_ora_updaterec_id = $result_array[0];
//获取updaterec中的最大id,用来生成系统数据
$sql = "select a.bpc_id,a.code as bpc_code,b.bpp_id,b.code as bpp_code,b.utime from BPC a,BPP b where b.bpc_id=a.bpc_id and b.utime>'$max_ora_bpp_id' order by b.bpp_id,a.bpc_id";
$result = mysql_query($sql, $Link);
$num = mysql_num_rows($result); //取得查询返回的记录行数
if ($num == 0)
die("没有需要同步的数据");
$errs = 0;
$down_time = strftime("%Y-%m-%d %H:%M:%S", time());
while ($row = mysql_fetch_array($result)) {
$sqlexist = "select count(*) from bid_code where bpp_id=" . $row["bpp_id"] . " and bpp_code='" . $row["bpp_code"] . "' and bpc_code='" . $row["bpc_code"] . "'";
$stmtexist = ociparse($g_dbh, $sqlexist);
ociexecute($stmtexist, OCI_DEFAULT);
ocifetchinto($stmtexist, & $result_arrayexist);
if ($result_arrayexist[0] == 0) {
$max_ora_updaterec_id = $max_ora_updaterec_id +1;
$sql = "insert into bid_code(bpc_id,bpc_code,bpp_id,bpp_code,utime) values(" . $row["bpc_id"] . ",'" . $row["bpc_code"] . "'," . $row["bpp_id"] . ",'" . $row["bpp_code"] . "',to_date('" . $row["utime"] . "','YYYY-MM-DD HH24:MI:SS'))";
$stmt = ociparse($g_dbh, $sql);
$err = OCIExecute($stmt, OCI_DEFAULT);
if (!$err)
$errs = $errs +1;
$sql = "insert into updaterec(\"ID\",\"CONTENT\",\"DATE\",\"DESC\",\"EDITION\") values(" . $max_ora_updaterec_id . ",'insert into bid_code(bpc_code,bpp_code) values(''" . $row["bpc_code"] . "'',''" . $row["bpp_code"] . "'')',to_date('$down_time','YYYY-MM-DD HH24:MI:SS'),'bidcode',0)";
$stmt = ociparse($g_dbh, $sql);
$err = OCIExecute($stmt, OCI_DEFAULT);
if (!$err)
$errs = $errs +1;
}
}
if ($errs > 0) {
ocirollback($g_dbh);
} else {
ocicommit($g_dbh);
echo "数据同步成功";
}
@ mysql_free_results($result);
@ mysql_close($Link);
?>
- 1
- 2
前往页