I have been trying to connect a database to generate XML File through PHP code, but I still have a problem on this code. Can somebody help me correcting this code? Thanks in advance.
<?php
@ $db = new mysqli('localhost','622Std1', '622StdDB1', '622stddb1');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database';
exit;
}
$query = "select * from bookmark";
$result = $db->query($query);
//$bookmarks[]=array('username'=>'user1','bm_URL'=>'http://msn.com');
//$bookmarks[]=array('username'=>'user2','bm_URL'=>'http://php.net');
//$bookmarks[]=array('username'=>'user3','bm_URL'=>'http://google.com');
//$bookmarks[]=array('username'=>'user4','bm_URL'=>'http://msn.com');
$num_results=mysqli_num_rows($result);
for($i=0;$i<$num_results;$i++)
{
$row = $result->fetch_assoc();
echo "<br/><br/>Username:";
echo stripslashes($row['username']);
echo "<br/>URL:";
echo stripslashes($row['bm_URL']);
$document=new DOMDocument();
$document->formatOutput=true;
$rootElement=$document->createElement("bookmarks");
$new_node=$document->appendChild($rootElement);
foreach($bookmarks as $key => $bookmarks)
{
$itemElement=$document->createElement("bookmark");
$Name=$document->createElement("username");
$Name->appendChild($document->createTextNode($row['username']));
$itemElement->appendChild($Name);
$BookMarkurl=$document->createElement("bm_URL");
$BookMarkurl->appendChild($document->createTextNode($row['bm_URL']));
$itemElement->appendChild($BookMarkurl);
$rootElement->appendChild($itemElement);
}
$document->save("GeneratedXMLFile.xml");
echo "Confirmed! the XML file is generated";
}
$db->close();
?>