使用PHP和MySQL输出XML但XML被破坏

时间:2013-03-13 17:55:32

标签: php mysql xml

我正在尝试使用PHP输出XML。从MySQL数据库表中收集数据并输出XML。我将这个用于Google Maps API Store Locator。只要从数据库表中检索到符合我查询的13行或更少行,它就可以正常工作。

只要我包含LIMIT 0,13,我就拥有并工作。

<?php
// Get parameters from URL
$products_input = $_GET["products"];

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("stores");
$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, user, password);
if (!$connection) {
    die("Not connected!");
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
    die ("Can\'t use db!");
}

// Search the rows in the stores table
$query = sprintf("SELECT * FROM stores WHERE products LIKE '%%%s%%' ORDER BY state LIMIT 0, 13",
    mysql_real_escape_string($products_input));
  $result = mysql_query($query);

if (!$result) {
    die("Invalid query!");
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
    $node = $dom->createElement("store");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("store", $row['store']);
    $newnode->setAttribute("address", $row['address']);
    $newnode->setAttribute("city", $row['city']);
    $newnode->setAttribute("state", $row['state']);
    $newnode->setAttribute("phone", $row['phone']);
    $newnode->setAttribute("lat", $row['lat']);
    $newnode->setAttribute("lng", $row['lng']);
}

echo $dom->saveXML();
?>

我得到了:

<?xml version="1.0"?>
<stores>
    <store store="Store A" address="1st St" city="City 1" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store B" address="2nd St" city="City 2" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store C" address="3rd St" city="City 3" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store D" address="4th St" city="City 4" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store E" address="5th St" city="City 5" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store F" address="6th St" city="City 6" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store G" address="7th St" city="City 7" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store H" address="8th St" city="City 8" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store I" address="9th St" city="City 9" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store J" address="10th St" city="City 10" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store K" address="11th St" city="City 11" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store L" address="12th St" city="City 12" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store M" address="13th St" city="City 13" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
</stores>

由于有超过13行符合我的查询,我想通过删除LIMIT 0,13或者使其更高的数字输出它们,但这就是我的XML中断的地方。

结果:

XML Parsing Error: unclosed token
Location: http://localhost:8888/storelocator/product/strsearch_genxml.php?products=harrow
Line Number 2, Column 4577:
<?xml version="1.0" encoding="ISO-8859-1"?>
<stores>
    <store store="Store A" address="1st St" city="City 1" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store B" address="2nd St" city="City 2" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store C" address="3rd St" city="City 3" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store D" address="4th St" city="City 4" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store E" address="5th St" city="City 5" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store F" address="6th St" city="City 6" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store G" address="7th St" city="City 7" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store H" address="8th St" city="City 8" state="SD" phone="+#.###.###.####" lat="##.######" lng="-###.######"/>
    <store store="Store I" address="9th St" city="City 9" state="SD" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store J" address="10th St" city="City 10" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store K" address="11th St" city="City 11" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store L" address="12th St" city="City 12" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store M" address="13th St" city="City 13" state="ND" phone="+#.###.###.####" lat="##.######" lng="-##.######"/>
    <store store="Store

关于我做错的任何想法?

0 个答案:

没有答案