Converting MySQL query to XML
Posted: Sat May 09, 2009 11:25 am
Hi, bit of a newbie to PHP and cannot work out why this does not work.
I can get it to create the XML as long as I do not have the closing tag in this line:
As soon as I add the closing tag it doesn't work.
Any help would be very much appreciated.
I can get it to create the XML as long as I do not have the closing tag in this line:
Code: Select all
$xml_output .= "\t\t<" . $colName . ">" . $nodeData . "</" . $colName . ">\n";Any help would be very much appreciated.
Code: Select all
$query = "SELECT * FROM listing";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$colLength = mysql_num_fields($resultID);
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<data>\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<listings>\n";
for($y = 0 ; $y < $colLength ; $y++){
$colName = mysql_field_name($resultID, $y);
$nodeData = $row[$colName];
$nodeData = str_replace("&", "&", $nodeData);
$nodeData = str_replace("<", "<", $nodeData);
$nodeData = str_replace(">", ">", $nodeData);
$nodeData = str_replace("\"", """, $nodeData);
$xml_output .= "\t\t<" . $colName . ">" . $nodeData . "</" . $colName . ">\n";
}
$xml_output .= "\t</listings>\n";
}
$xml_output .= "</data>";
echo $xml_output;
?>