0

A weird error is occuring when I execute my test HTML file. My code is below:

<html>
 <head>
   <title>PHP Test</title>
 </head>
 <body>
 <?php
mysql_connect('myip', '----', '----');
mysql_select_db('test');

$sql = "SELECT 1st, 3rd FROM test";
$res = mysql_query($sql);

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);

$xml->startElement('dataset');

while ($row = mysql_fetch_assoc($res)) {
  $xml->startElement("data");

      $xml->writeAttribute('unit', $row['1st']);
  $xml->writeAttribute('value', $row['3rd']);

  //$xml->writeRaw($row['country']);

  $xml->endElement();
}

$xml->endElement();

header('Content-type: text/xml');
$xml->flush();
?>
 </body>
</html>

After I executed it, it went error after $xml = new XMLWriter(); like this:

https://i.stack.imgur.com/RTQ6K.png

It only shows pure text after I newed an XmlWriter object.

If I made any mistake please let me know and sorry for my bad English.

This is the error shows when I try to access php file from localhost. Imgur

sao
  • 1,835
  • 6
  • 21
  • 40
  • I don't understand...Do you mean I should put the files under XAMPP folder and access it through Internet? I tried and it still returns the same result. BTW, I try to access only php file and it returns "Extra content at the end of the document" error T^T – Sherl_NKNU Mar 27 '15 at 08:25
  • .html -> .php. See as well this reference answer please: [Code doesn't run/what looks like parts of my PHP code are output](http://stackoverflow.com/a/24423272/367456) - the problem you have has nothing to do specifically with XMLWriter. Please let me know if this information was helpful for you. – hakre Mar 27 '15 at 21:14
  • @hakre Yes, I know what you mean now. However, as I said in previous comment, I accessed my php file through Internet and it returns "Extra content at the end of the document" error. It must be some bug in my code T_T The original php code is copied from [another stackoverflow question](http://stackoverflow.com/questions/5112282/get-mysql-database-output-via-php-to-xml), I only did some edit to match my output. Could you help me to debug it? – Sherl_NKNU Mar 28 '15 at 08:23

0 Answers0