SQL导出到XML。使用PHP脚本。 (客户出口)

时间:2013-07-05 09:01:35

标签: php sql xml export

我的查询结果有问题。当我运行它时,它会批量查询它。 我需要从每个客户那里开辟新的头脑。

我认为问题是从第143行到第179行。

if (!$_HTTP_POST_VARS["separator"])

$sep= str_replace('\t', "\011", $sep);
//$contents="customers_id".$sep."customers_lastname".$sep."customers_firstname".$sep."customers_email_address".$sep."customers_gender".$sep."customers_dob".$sep."entry_company".$sep."entry_street_address".$sep."entry_postcode".$sep."entry_city".$sep."entry_state".$sep."entry_suburb".$sep."countries_name".$sep."customers_telephone".$sep."customers_fax\n";
$customers_query_raw = "select c.customers_id,
                                  c.customers_lastname,
                                  c.customers_firstname,
                                  c.customers_email_address,
                                  c.customers_gender,
                                  c.customers_dob,
                                  c.customers_telephone,
                                  c.customers_fax,
                                  a.entry_company,
                                  a.entry_street_address,
                                  a.entry_postcode,
                                  a.entry_city,
                                  a.entry_state,
                                  a.entry_suburb,
                                  co.countries_name
                                   from " . TABLE_CUSTOMERS . " c left join " . TABLE_ADDRESS_BOOK . " a on c.customers_id = a.customers_id and c.customers_default_address_id = a.address_book_id
                                   left join " . TABLE_COUNTRIES . " co on co.countries_id = a.entry_country_id
                                   WHERE c.customers_id >= 4150 and c.customers_id <= 4200";
    $customers_query = tep_db_query($customers_query_raw) or die(mysql_error());
    while ($row = tep_db_fetch_array($customers_query)) {

                $customers_id.=$row['customers_id'];
                                $customers_lastname.=$row['customers_lastname'];                    
                                $customers_firstname.=$row['customers_firstname"'];
                                $customers_email_address.=$row['customers_email_address'];
                                $customers_gender.=$row['customers_gender'];
                                $customers_dob.=$row['customers_dob'];
                                $entry_company.=$row['entry_company'];
                                $entry_street_address.=$row['entry_street_address'];
                                $entry_postcode.=$row['entry_postcode'];
                                $entry_city.=$row['entry_city'];
                                $entry_state.=$row['entry_state'];
                                $entry_suburb.=$row['entry_suburb'];
                                $countries_name.=$row['countries_name'];
                                $customers_telephone.=$row['customers_telephone'];
                                $customers_fax.=$row['customers_fax'];                            
            }

    /*print('<?xml version="1.0" encoding="ISO-8859-1" ?>');*/
    header("Content-type: text/html; charset=ISO-8859-1");
    header("Content-disposition: attachment; filename=ordre_export_" . date("Ymd") . ".xml");
    header("Content-type: application/octet-stream; charset=iso-8859-1");


        echo '<?xml version="1.0" encoding="iso-8859-1"?>
        <!--  -->
        <Root xmlns:dt="urn:schemas-microsoft-com:datatypes">
        ';  

        echo "
        <head>
            <Kundenr>$customers_id</Kundenr>
            <Company>$entry_company</Company>
            <Kundenavn>$customers_lastname  </Kundenavn>
            <Adresse>$entry_street_address</Adresse>
            <Adresse2> </Adresse2>
            <Postnr>$entry_postcode</Postnr>
            <Poststed>$entry_city</Poststed>
            <Landkode> </Landkode>
            <Land>$countries_name</Land>
            <customers_telephone>$customers_telephone</customers_telephone>
            <fax>$customers_fax</fax>
            <mail>$customers_email_address</mail>
            <dob>$customers_dob</dob>
            <Fritekst></Fritekst>
            <Kommentarer></Kommentarer>
        </head>
        ";
                                $j++;

    echo "
    </Root>";
    die();
}
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>

它如何出来的样本。我的客户ID是4位数。所以它应该在4150之后制动并启动一个新的XML块。

<?xml version="1.0" encoding="iso-8859-1"?>
        <!--  -->
        <Root xmlns:dt="urn:schemas-microsoft-com:datatypes">

        <head>
            <Kundenr>41504151415241534154415541564157415841594160416141670417</Kundenr>

2 个答案:

答案 0 :(得分:1)

您正在使用连接运算符,因此它会为您的所有查询结果生成一个长字符串:

$customers_id.=$row['customers_id'];

您可能希望将该数据放入数组中:

$customer['id'][$key] = $customers_id.=$row['customers_id'];

答案 1 :(得分:0)

你的错误在于连接所有行的结果。环

while ($row = tep_db_fetch_array($customers_query))

应该用于输出像

这样的XML条目
echo "
    <head>
        <Kundenr>$row[customers_id]</Kundenr>
        ...
    </head>
";

请更改标题以匹配实际内容类型(这不是问题的原因,但很重要):

header("Content-type: text/xml; charset=ISO-8859-1");