gpt4 book ai didi

php - 从xml文件中检索后,将数据插入不同行的mysql php

转载 作者:行者123 更新时间:2023-11-29 06:59:47 24 4
gpt4 key购买 nike

test.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<propertyList>
<business type="food">
<listingAgent id="1">
<name>Spiro Abelas</name>
<email>spiro@abelas.com.au</email>
</listingAgent>
</business>
<business type="food">
<listingAgent id="2">
<name>andy</name>
<email>andy@abelas.com.au</email>
</listingAgent>
</business>
</propertyList>

Php 代码 将 XML 文件解释为对象

<?php
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
echo "<pre>";
print_r($xml);
echo "</pre>";
} else {
exit('Failed to open test.xml.');
}
?>

输出

     SimpleXMLElement Object
(
[business] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => food
)
[listingAgent] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[name] => spiro
[email] => spiro@abelas.com.au
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => food
)
[listingAgent] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2
)
[name] => andy
[email] => andy@abelas.com.au
)
)))

表结构

enter image description here

我想将这样的数据插入到表中

enter image description here

但我不知道如何从数组中检索数据并插入多行

所以请指导我如何解决它

最佳答案

foreach($xml->business as $row)
{
$business_type = $row->attributes()->type;
$id = $row->listingAgent->attributes()->id;
$name = $row->listingAgent->name;
$email = $row->listingAgent->email;
mysql_query('INSERT INTO table…');
}

关于php - 从xml文件中检索后,将数据插入不同行的mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10799735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com