如何使用PHP将XML字符串中的值插入到mysql中?

时间:2015-05-07 23:41:58

标签: php mysql arrays xml import

我正在尝试使用以下代码读取和解析XML文件但是我在运行int几个问题时第一个问题是状态变量和其他已知的填充变量没有导入到MYSQL表中,不确定为什么。我的所有表和列都是varchar(255)。

第二个问题是记录总数停在50,没有任何错误或任何错误。不知道为什么单独的CA状态有数百条记录,这个脚本应该循环遍历所有状态,然后循环遍历XML字符串中的所有项目并将它们插入到MYSQL表中。

我有点困惑任何帮助表示赞赏。

<?php

$arr = array("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY");
foreach ($arr as $value) {

    $url ='http://api.votesmart.org/Address.getOfficeByOfficeState?key=04c935d4337616f104e5fc905e9fef2d&officeId=9&stateId='.$value.'&officeTypeId=C';
        $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url); //getting url contents

    $data = curl_exec ($ch); //execule curl request
    curl_close($ch);

    // read XML data string
    $xml = simplexml_load_string($data) or die("ERROR: Cannot create SimpleXML object");
    // open MySQL connection
    $connection = mysqli_connect("localhost", "xxx", "xxx", "xxx") or die ("ERROR: Cannot connect");
    // process node data
    // create and execute INSERT queries

    foreach ($xml->office->candidate as $item) {
        foreach ($xml->office->address as $item2) {
            foreach ($xml->office->phone as $item3) {
                foreach ($xml->office->notes as $item4) {

                    $candidateId = $item->candidateId;
                    $title = mysqli_real_escape_string($connection, $item->title);
                    $firstName = mysqli_real_escape_string($connection, $item->firstName);
                    $middleName = mysqli_real_escape_string($connection, $item->middleName);
                    $nickName = mysqli_real_escape_string($connection, $item->nickName);
                    $lastName = mysqli_real_escape_string($connection, $item->lastName);
                    $suffix = mysqli_real_escape_string($connection, $item->suffix);

                    $type = $item2->type;
                    $typeId = $item2->typeId;
                    $street = $item2->street;
                    $city = $item2->city;
                    $state = $item->state;
                    $zip = $item2->zip;

                    $phone1 = $item3->phone1;
                    $phone2 = $item3->phone2;
                    $fax1 = $item3->fax1;
                    $fax2 = $item3->fax2;
                    $tollFree = $item3->tollFree;
                    $ttyd = $item3->ttyd;
                    $cellphone = $item3->cellphone;

                    $contactName = $item4->contactName;
                    $contactTitle = $item4->contactTitle;

                    $sql = "INSERT INTO address (candidateId, title, firstName, middleName, nickName, lastName, suffix, type, typeId, street, city, state, zip, phone1, phone2, fax1, fax2, tollFree, ttyd, cellphone, contactName, contactTitle) VALUES ('$candidateId', '$title', '$firstName', '$middleName', '$nickName', '$lastName', '$suffix', '$type', '$typeId', '$street', '$city', '$state', '$zip', '$phone1', '$phone2', '$fax1', '$fax2', '$tollFree', '$ttyd', '$cellphone', '$contactName', '$contactTitle') ON DUPLICATE KEY UPDATE title = '$title', firstName = '$firstName', middleName = '$middleName', nickName = '$nickName', lastName = '$lastName', suffix = '$suffix', type = '$type', typeId = '$typeId', street = '$street', city = '$city', state = '$state', zip = '$zip', phone1 = '$phone1', phone2 = '$phone2', fax1 = '$fax1', fax2 = '$fax2', tollFree = '$tollFree', ttyd = '$ttyd', cellphone = '$cellphone', contactName = '$contactName', contactTitle = '$contactTitle'";

                    mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");
                }
            }
        }
    }

    // close connection
    mysqli_close($connection);
}

?>

1 个答案:

答案 0 :(得分:0)

乍一看我不能说你的数据是否存在问题,但是对于你做的foreach迭代,有一个问题:

foreach ($xml->office->candidate as $item) {
        foreach ($xml->office->address as $item2) {

$xml->office->candidate之外的外部迭代,导致$item代表第一个 office 元素的所有候选元素。也许这个数字最有可能在50时止,因为有54个州,也许并非所有州都有办公室。

快速检查确实显示这54个文件的第一个办公室元素中有50个候选条目。

所以你更有可能想要以不同的方式堆叠foreach-es。

foreach ($xml->office as $office) {
    foreach ($office->candidate as $candidate) {
        ...
    }
}

仅此级别已经在这些级别中显示了那些54 XML中所有 office 元素(而不仅仅是第一个)中的3383个候选元素单独的文件。

当你将这些foreach-es的多个级别堆叠在一起时,这可能变得非常复杂,所以你应该从外部到内部构建它 - 这需要一些规划和一些逐步的工作。

如果您希望从内部构建到外部,我建议使用Xpath来首先获取感兴趣的元素节点,然后使用其他相对xpath查询获取父代的信息。但这需要xpath中的知识,我不知道你是否听说过它。

无论如何,关注foreach-es并且最有可能选择更好的变量名称应该让你到达目的地。