I’m trying to get the output mysql as xml using php tutorial to work, but I’m having one problem with it.
since a php file starts off as <?php…and ends as ?> when ever I’m declaring that the output is gonna be xml, like this:
$xml_output = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
";
it misunderstands the "?>" in the end of the xml declaration as the closing php tag.
here’s all of php code:
<?php
header("Content-type: text/xml");
$host = "localhost";
$user = "root";
$pass = "pass";
$database = "wgshop";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to hose.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT * FROM shopproducts ORDER BY id DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\"?>
";
$xml_output .= "<store>
";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++) {
$row = mysql_fetch_assoc($resultID);
$xml_output .= " <products>
";
//Escaping illegal characters
$row['prodName'] = str_replace("&, "&", $row['prodName']);
$row['prodName'] = str_replace("<", "<". $row['prodName']);
$row['prodName'] = str_replace(">", ">", $row['prodName']);
$row['prodName'] = str_replace("\"", """, $row['prodName']);
$xml_output .= " <prodName>" . $row['prodName'] . "</prodName>
";
//Escaping illegal characters
$row['price'] = str_replace("&, "&", $row['price']);
$row['price'] = str_replace("<", "<". $row['price']);
$row['price'] = str_replace(">", ">", $row['price']);
$row['price'] = str_replace("\"", """, $row['price']);
$xml_output .= " <price>" . $row['price'] . "</price>
";
$xml_output .= " <section>" . $row['section'] . "</section>
";
$xml_output .= " <prodImage>" . $row['prodImage'] . "</prodImage>
";
//Escaping illegal characters
$row['description'] = str_replace("&, "&", $row['description']);
$row['description'] = str_replace("<", "<". $row['description']);
$row['description'] = str_replace(">", ">", $row['description']);
$row['description'] = str_replace("\"", """, $row['description']);
$xml_output .= " <description>" . $row['description'] . "</description>
";
$xml_output .= " </products>
";
}
$xml_output .= "</store>";
echo $xml_output;
?>
I know that it’s that “?>” in the xml declaration that messing it all up, but I’m a php novice so I have no clue what to do about it.
I’ve searched the boards and noone else seems to be having this problem.