本地服务器和远地服务器上都有一个字段几乎相同的数据库表。 现在本地服务器需要定期接收远地服务器发送过来的XML数据文件,使用php读取xml中的数据,然后转存到本地服务器数据库表中。 下面是一个XML范例test.xml
<?xml version="1.0" encoding="gb2312" ?>
<res>
<is_success result="true" />
<order_list count="1">
<order>
<order_time>20091213 075817</order_time>
<merchant_id>luxi</merchant_id>
<order_code>209121301580</order_code>
<stat>yes</order_code>
</order> <order>
<order_time>20091113 072317</order_time>
<merchant_id>jihu</merchant_id>
<order_code>318101301281</order_code>
<stat>no</order_code>
</order>
</order_list>
</res>数据库中有字段order_time,merchant_id,order_codestat,本地数据库表通过字段order_code和merchant_id来标识唯一记录,XML转存MySQL过程中可能会执行以下操作:
1、搜索MySQL数据库表记录;
2、如果记录不存在,则插入;
3、如果表中已存在记录,则进行 stat 字段值比较,如果 stat 字段值已发生变化,则更改字段值。php读取XML的代码我已经写好
$doc = simplexml_load_file('test.xml');foreach ($doc->order_list->order as $order) {
/* 获取xml中的数据 */
$order_time = $order->order_time;
$merchant_id = $order->merchant_id;
$order_code = $order->order_code;
$stat = $order->stat; /* 转存 MySQL 代码..... */
}
MySQL数据库转存操作可通过 REPLACE 语句或者insert ... on duplicate update 来实现,这个我不是很熟,不知各位能否帮忙写段php代码?
<?xml version="1.0" encoding="gb2312" ?>
<res>
<is_success result="true" />
<order_list count="1">
<order>
<order_time>20091213 075817</order_time>
<merchant_id>luxi</merchant_id>
<order_code>209121301580</order_code>
<stat>yes</order_code>
</order> <order>
<order_time>20091113 072317</order_time>
<merchant_id>jihu</merchant_id>
<order_code>318101301281</order_code>
<stat>no</order_code>
</order>
</order_list>
</res>数据库中有字段order_time,merchant_id,order_codestat,本地数据库表通过字段order_code和merchant_id来标识唯一记录,XML转存MySQL过程中可能会执行以下操作:
1、搜索MySQL数据库表记录;
2、如果记录不存在,则插入;
3、如果表中已存在记录,则进行 stat 字段值比较,如果 stat 字段值已发生变化,则更改字段值。php读取XML的代码我已经写好
$doc = simplexml_load_file('test.xml');foreach ($doc->order_list->order as $order) {
/* 获取xml中的数据 */
$order_time = $order->order_time;
$merchant_id = $order->merchant_id;
$order_code = $order->order_code;
$stat = $order->stat; /* 转存 MySQL 代码..... */
}
MySQL数据库转存操作可通过 REPLACE 语句或者insert ... on duplicate update 来实现,这个我不是很熟,不知各位能否帮忙写段php代码?
$doc = simplexml_load_file('test.xml');foreach ($doc->order_list->order as $order) {
/* 获取xml中的数据 */
$order_time = $order->order_time;
$merchant_id = $order->merchant_id;
$order_code = $order->order_code;
$stat = $order->stat; /* 转存 MySQL 代码..... */
$insertSQL = "REPLACE INTO members (order_time, merchant_id, order_code, stat) VALUES ('$order_time ', '$merchant_id', '$order_code', '$stat')";
$Result1 = mysql_query($insertSQL, $member) or die(mysql_error());
}
这样可以即时更新数据,
比你先生成xml再解析再入库效率和安全性上都是非常大的提高.详细配置,参看我的blog
mysqld_muti配置运行mysql多服务