Sunday, April 17, 2011

Get the Mysql records in Xml format

Introduction
.................................................................................
This tutorial will show you how to take data that is stored in your mySQL database and easily turn it into XML.

$query ="select* from userdetails";
$resultID = mysql_query($query) or die(mysql_error());


$num = mysql_num_rows($resultID) ;
 if ($num != 0) {

 $file= fopen("xml/results.xml","w");

 $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";

 $_xml .="<userdetails>\r\n";


while ($row = mysql_fetch_array($resultID)) {


$_xml .="\t<user>\r\n";
$_xml .="\t" . $row["userid"] . "\r\n";

$_xml .="\t\t" . $row["username"] . "\r\n";
$_xml .="\t</user>
\r\n";

}

$_xml .="</userdetails>
";

fwrite($file, $_xml);

fclose($file);

//echo "XML has been written";


} else {

echo "No Records found";

}


?>

No comments:

Post a Comment