简体   繁体   中英

MySQL to XML using PHP script

I have a situation where I am running a PHP script in order to output the contents of a MYSQL database to XML. For some reason, the below code generates the desired result for the first 7 rows of the database and then it fails with the following error:

This page contains the following errors: error on line 1 at column 860: Encoding error

Below is a rendering of the page up to the first error."

(I am using the 'ID' column as a reference)

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysqli_query($database, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error());
}

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

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'id="' . $ind . '" ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}

// End XML file
echo '</markers>';

The desired output here would be:

<markers>
<marker id="1" name="xxx" address="xxx" lat="xxx" lng="xxx" type="xxx"/>
<marker id="2" name="xxx" address="xxx" lat="xxx" lng="xxx" type="xxx"/>
...
</markers>

Having stared at this for hours I can't seem to figure it out, would love another set of eyes to have a look as I don't think there is anything necessarily wrong with the code, but was wondering whether there is some limitation in the method implemented?

Thanks in advance all, have a great day😊

This code works just fine in order to convert MYSQL to XML with PHP, however there was an error in the 8th row as suggested by Steve in the comments. Always check your apostrophes folks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM