using php to create an xml file from a mysql db
im trying to create a xml file using php.everytime i run the code the page displayes the code from a certain point as text on the screen.the code i hav is as follows:
&开发者_如何转开发lt;?php
if(!$dbconnet = mysql_connect('I took out the details')){
echo "connection failed to the host.";
exit;
}
if (!mysql_select_db('siamsati_db')){
echo "Cannot connect to the database.";
exit;
}
$table_id = 'events';
$query = "SELECT * FROM $table_id";
$dbresult = mysql_query($query, $dbconnect);
$doc = new DomDocument('1.0');
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($dbresult)){
$ooc = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
foreach ( $row as $fieldname => $fieldvalue){
$child = $doc->createElement($fieldname);
$child = $occ->appendchild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
$xml_string = $doc->saveXML();
echo $xml_string;
?>
and the page when displayed shows:
createElement('root'); $root = $doc->appendChild($root); while($row = mysql_fetch_assoc($dbresult)){ $ooc = $doc->createElement($table_id); $occ = $root->appendChild($occ); foreach ( $row as $fieldname => $fieldvalue){ $child = $doc->createElement($fieldname); $child = $occ->appendchild($child); $value = $doc->createTextNode($fieldvalue); $value = $child->appendChild($value); } } $xml_string = $doc->saveXML(); echo $xml_string; ?>
is there something ive missed.ive checked all the quotes thinking it was that at first but they all seem to be right.any suggestions on what im doing wrong are much appreciated?
Set the content type to be XML, so that the browser will recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );
In your code Change it to:
// Set the content type to be XML, so that the browser will recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );
// "Create" the document.
$doc = new DOMDocument( "1.0", "ISO-8859-15" );
+++I think you can do something like this
<root>
<?
foreach ( $row as $fieldname => $fieldvalue){
?>
<events>
<fieldname><?=fieldname; ?></fieldname>
<fieldvalue><?=$fieldvalue; ?></fieldvalue>
</events>
}
?>
</root>
In the code you've posted here the initial <?php
tag is missing...
精彩评论