将数组数据插入mysql php [英] Insert array data into mysql php
本文介绍了将数组数据插入mysql php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 test.xml 文件
<business modTime="2011-10-31-12:33:07" status="sold"><agentID>TEST</agentID><uniqueID>92134</uniqueID><listingAgent id="1"><name>Spiro Abelas</name><电话类型="BH"></电话><电话类型="手机">0414298899</电话><email>spiro@abelas.com.au</email></listingAgent><listingAgent id="2"></listingAgent><地址显示=否"><state>NSW</state><邮政编码>2203</邮政编码><国家>澳大利亚</国家></地址></业务></propertyList>
我确实像那样分析了完全的 XML
$value){echo "..1..[$key0] => $value";foreach($value->attributes() as $attributeskey0 => $attributesvalue1){echo "________[$attributeskey0] = $attributesvalue1";}回声'<br/>';////////////////////////////////////////////////foreach($value as $key => $value2){echo "....2.....[$key] => $value2";foreach($value2->attributes() as $attributeskey => $attributesvalue2){echo "________[$attributeskey] = $attributesvalue2";}回声'<br/>';///////////////////////////////////////////////foreach($value2 作为 $key2 => $value3){回声.........3..........[$key2] => $value3";foreach($value3->attributes() as $attributeskey2 => $attributesvalue3){echo "________[$attributeskey2] = $attributesvalue3";}回声'<br/>';////////////////////////////////////////////////}}回声'<br/>';}?>
获取这些输出
..1..[business] =>________[modTime] = 2011-10-31-12:33:07________[状态] = 已售出....2.....[agentID] =>测试....2 .....[uniqueID] =>92134....2.....[listingAgent] =>________[id] = 1.....3..........[名称] =>斯皮罗·阿贝拉斯.....3..........[电话] =>________[类型] = BH.....3..........[电话] =>0414298899________[类型] = 手机.....3..........[电子邮件] =>spiro@abelas.com.au....2.....[listingAgent] =>________[id] = 2....2 .....[地址] =>________[显示] = 否.....3..........[状态] =>新南威尔士州.....3..........[邮政编码] =>2203.....3..........[国家] =>澳大利亚
我想像这样触发查询(现在它是静态的,但我想让它变得动态)
insert into xml(business,agentID,uniqueID,listingAgent,name,telephone,email,state,postcode,country)values('sold', 'TEST', 92134, 1, 'Spiro Abelas', 0414298899, 'spiro@abelas.com.au', 'NSW', '2203', '澳大利亚')
所以请指导如何从数组中检索数据并插入到表中
解决方案
以下代码会将您的 XML 转换为数组.
$xml = '<business modTime="2011-10-31-12:33:07" status="sold"><agentID>TEST</agentID><uniqueID>92134</uniqueID><listingAgent id="1"><name>Spiro Abelas</name><电话类型="BH"></电话><电话类型="手机">0414298899</电话><email>spiro@abelas.com.au</email></listingAgent><listingAgent id="2"></listingAgent><地址显示=否"><state>NSW</state><邮政编码>2203</邮政编码><国家>澳大利亚</国家></地址></业务></propertyList>';$xml = simplexml_load_string($xml);$json = json_encode($xml);$array = json_decode($json,TRUE);
你可以执行上面的代码,并像下面这样打印数据
print"";打印_r($array);print_r($array['business']['listingAgent']);打印"</pre>";
您可以处理 $array 并保存在表中.
i have test.xml file
<propertyList date="2012-05-21-17:12:37" username="" password="">
<business modTime="2011-10-31-12:33:07" status="sold">
<agentID>TEST</agentID>
<uniqueID>92134</uniqueID>
<listingAgent id="1">
<name>Spiro Abelas</name>
<telephone type="BH"></telephone>
<telephone type="mobile">0414298899</telephone>
<email>spiro@abelas.com.au</email>
</listingAgent>
<listingAgent id="2"></listingAgent>
<address display="no">
<state>NSW</state>
<postcode>2203</postcode>
<country>Australia</country>
</address>
</business>
</propertyList>
And i did Analyze fully XML like that
<?php
$xml = simplexml_load_file('test.xml');
foreach($xml as $key0 => $value){
echo "..1..[$key0] => $value";
foreach($value->attributes() as $attributeskey0 => $attributesvalue1){
echo "________[$attributeskey0] = $attributesvalue1";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value as $key => $value2){
echo "....2.....[$key] => $value2";
foreach($value2->attributes() as $attributeskey => $attributesvalue2){
echo "________[$attributeskey] = $attributesvalue2";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value2 as $key2 => $value3){
echo ".........3..........[$key2] => $value3";
foreach($value3->attributes() as $attributeskey2 => $attributesvalue3){
echo "________[$attributeskey2] = $attributesvalue3";
}
echo '<br />';
////////////////////////////////////////////////
}}
echo '<br />';
}
?>
Getting those output
..1..[business] => ________[modTime] = 2011-10-31-12:33:07________[status] = sold
....2.....[agentID] => TEST
....2.....[uniqueID] => 92134
....2.....[listingAgent] => ________[id] = 1
.........3..........[name] => Spiro Abelas
.........3..........[telephone] => ________[type] = BH
.........3..........[telephone] => 0414298899________[type] = mobile
.........3..........[email] => spiro@abelas.com.au
....2.....[listingAgent] => ________[id] = 2
....2.....[address] => ________[display] = no
.........3..........[state] => NSW
.........3..........[postcode] => 2203
.........3..........[country] => Australia
i want to need to fire query like(now it's static but i want make it dyanmic)
insert into xml(business,agentID,uniqueID,listingAgent,name,telephone,email,state,postcode,country)values('sold', 'TEST', 92134, 1, 'Spiro Abelas', 0414298899, 'spiro@abelas.com.au', 'NSW', '2203', 'Australia')
so plz guide to how to retrieve data from array and insert into table
解决方案
Following code will convert your XML to Array.
$xml = '<?xml version="1.0" encoding="utf-8"?>
<propertyList date="2012-05-21-17:12:37" username="" password="">
<business modTime="2011-10-31-12:33:07" status="sold">
<agentID>TEST</agentID>
<uniqueID>92134</uniqueID>
<listingAgent id="1">
<name>Spiro Abelas</name>
<telephone type="BH"></telephone>
<telephone type="mobile">0414298899</telephone>
<email>spiro@abelas.com.au</email>
</listingAgent>
<listingAgent id="2"></listingAgent>
<address display="no">
<state>NSW</state>
<postcode>2203</postcode>
<country>Australia</country>
</address>
</business>
</propertyList>
';
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
you can execute above code and prinf data like below
print"<pre>";
print_r($array );
print_r($array['business']['listingAgent'] );
print"</pre>";
You can Process $array and save in table.
这篇关于将数组数据插入mysql php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文