【问题标题】:'simplexml' php to mysql database loop return only first element'simplexml' php 到 mysql 数据库循环仅返回第一个元素
【发布时间】:2019-01-13 20:08:02
【问题描述】:

感谢您对此提供的帮助。我有一个很长的 xml 表单,其中包含 80 多个要插入到我的数据库中的项目。

我对此进行了研究,但由于某种原因,我的 foreach 循环不起作用。

我已经减少了此处的插入内容,以便了解我想要做什么。

我可以将第一个 'property / item 插入到数据库中,所以我知道插入等没有问题。

由于某种原因,循环不会显示数据库中的其他 79 个项目。

$affectedRow = 0;

$xml =  simplexml_load_file('properties2.xml') or die("can not find it");

foreach($xml->children()  as $row) {    
    $reference = $row->branch->properties->property['reference'];
    $instructedDate = $row->branch->properties->property->instructedDate;
    $price_text = $row->branch->properties->property->price_text;

    $sql = "INSERT INTO test( reference, instructedDate, price_text) VALUES ('". $reference ."','". $instructedDate ."','". $price_text ."')";

    $result = mysqli_query($conn, $sql);

    if (! empty($result )) {
        $affectedRow ++;
    } else {
        $error_message = mysqli_error($conn) . "\n";
    }
}
?>

例如xml文件

-<agency branchName="billies Estate Agents " name="billie ea" xsi:noNamespaceSchemaLocation="http://www.feedcompany.co.uk/xmlexport/feedcompanyXMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    -<branches>

        -<branch name="billies Estate Agents ">

            -<properties>

                -<property reference="00000477">

                    <instructedDate>26/02/2018 15:11:56</instructedDate>

                    <price_text>Offers in Excess of £200,000</price_text>

                 </property

               -<property reference="00000478">

                    <instructedDate>26/02/2018 15:11:56</instructedDate>

                    <price_text>Offers in Excess of £200,000</price_text>

                </property>

【问题讨论】:

  • 如果您只是尝试显示循环中的值,您会看到所有这些值吗?您能否在您的问题中添加一个 XML 文件的简短示例,以便其他人可以测试(不需要或想要所有 80 个项目)?
  • 始终在开发和测试代码时,在 PHP 中启用错误显示。如果未找到 XML 引用(因为 children() 未完全返回您所期望的),您可能在循环中遇到致命错误。在脚本的顶部error_reporting(E_ALL); ini_set('display_errors', 1);
  • 请注意,这容易受到二次 SQL 注入的影响。使用 mysqli,您应该使用prepare()/bind_param(),execute()。有关示例,请参阅How can I prevent SQL injection in php
  • 戴夫,我如何显示这些值?你的意思是呼应他们?迈克尔,当我粘贴时没有任何反应.. 我使用 error_message 显示出现问题时,即没有变量匹配或拼写错误等
  • 是的,我可以在页面上回显我想要的每个元素...但仅适用于第一个属性...没有循环

标签: php mysql xml loops foreach


【解决方案1】:

您只能将第一个元素放入数据库,因为 $xml-&gt;children() 不是您所期望的。

请注意,您的 XML 以 &lt;agency&gt; 开头,然后是 &lt;branches&gt; 标记。我猜你的完整 XML 是这样的:

<agency>
    <branches>
        <branch>
            <properties>
                <property>
                    ...
                </property>
                <property>
                    ...
                </property>
            </properties>
        </branch> 
    </branches>
</agency>

你想获取所有属性 -> 所以你需要使用$xml-&gt;branches-&gt;branch-&gt;properties-&gt;children()

类似:

foreach($xml->branches->branch->properties->children() as $property) {
    $sql = "INSERT INTO test( reference, instructedDate, price_text) VALUES ('". $property['reference']."','". $property->instructedDate ."','". $property->price_text."')";
 ...
 } 

当您像在第 3 行中那样执行 $xml-&gt;children() 时,您会将分支标记作为数组中的唯一元素 - 这就是为什么您只有一个元素插入到您的数据库中的原因。

【讨论】:

  • 嗨,它适用于 87 个元素,但它们是相同的条目。即不是 87 个单独的属性。我应该在某个地方有一个 i++ 计数器来循环遍历每个属性吗?
  • fooreach 代码是伪代码。您需要从您的 xml 中获取属性标签的数组并在他身上运行循环。如果您这样做,那么数组中的每个元素都是单个属性 - 现在您可以直接访问属性数据。不需要 i 计数器 - 只需使用 xpath 获取数组
  • 我现在已经进行了正确的测试,而不是仅仅试图强迫你的代码进入我已经很糟糕的工作; .有效 。太棒了,我在网上找不到一个接近的 tuts,而你的逻辑要简单 10 倍......非常感谢。
  • 嗨,大卫.. 很抱歉,您的代码运行良好...您在最后一条评论中在说什么?伪?你是在说如果我还没有做过那我会做什么吗?所以因为我使用了你的代码并且它有效,我应该用过去时而不是未来来阅读?
  • 我所说的伪是我的 foreach $xml-&gt;branches-&gt;branch-&gt;properties-&gt;children() 中的数组没有经过测试,我的意思是提取属性数组。如果可行 - 很好,如果没有,那么您可以使用 xpath 提取此数组
猜你喜欢
  • 2019-10-30
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2017-03-29
  • 2016-02-22
  • 2022-11-21
  • 1970-01-01
相关资源
最近更新 更多