Code: Select all
<?
require_once('classes/database.class.php');
$connection = new database();
$link = $connection->database_connection();
$table_id = 'country';
$query = "select * from country";
$results = mysql_query($query) or die(mysql_error());
if (!$results)
{
print 'There was a database error when executing';
print mysql_error();
exit;
}
//create new xml document
$doc = new DomDocument('1.0' , 'ISO-8859-1');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
//process one row at a time
while($row = mysql_fetch_assoc($results)) {
//add node for each row
$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
//add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} //foreach
} //while
$xml_string = $doc->saveXML();
echo $xml_string