我尝试从数据库中的此XML结构获取每个文章编号:
<Article Key="8075581" Status="active"> ... </Article>
<Article Key="8050939" Status="active"> ... </Article>
<Article Key="8047871" Status="active"> ... </Article>
<Article Key="8058626" Status="active"> ... </Article>
首先,我尝试获取属性“ Key”的值并将其插入到我的数据库中:
<?php
<...>
$xmlstring = simplexml_load_file("export.xml");
foreach($xmlstring->children() as $article) {
$articleid = (string) $article['Key'];
}
$sql = "INSERT INTO articles (articelnr)
VALUES ($articleid)";
<...>
$conn->close();
但这只是插入最后一个值:
ID KEY
01 8058626
有人可以帮助我在数据库中单独获取每个“密钥”吗?像这样:
ID KEY
01 8075581
02 8050939
03 8047871
04 8058626
谢谢
答案 0 :(得分:0)
<?php
<...>
$xmlstring = simplexml_load_file("export.xml");
foreach($xmlstring->children() as $article) {
$articleid = (string) $article['Key'];
$sql = "INSERT INTO articles (articelnr) VALUES ($articleid)";
if ($conn->query($sql) === TRUE) {
echo "\n\n### DONE ###\n\n";}
else {
echo "\n\n### ERROR ###" . $sql . "\n" . $conn->error . "\n\n";}
}
<...>
$conn->close();
工作了!我已将insert方法放入foreach中。