0

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😊

8
  • Possible duplicate of Correct transfer of MYSQL to XML using PHP Commented May 3, 2017 at 14:33
  • Paste the generated xml (right click and select view source). Something in the 8th row is breaking the xml, probably a quote or backslash Commented May 3, 2017 at 14:39
  • Where does variable $ind magically come from? Do you mean $row['id'] Commented May 3, 2017 at 14:48
  • Dont use the @ error silencer. If you got errors FIX 'EM Commented May 3, 2017 at 14:49
  • Top tip for getting answers on SO, especially in a very busy tag like php. Make sure you are available to address comments straight away, because most potential answerers will have forgotten your question exists within half an hour or so. If you do revisit this question, @ reply me so i get notified. Commented May 3, 2017 at 15:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.