Wednesday, February 22, 2012

Create XML file using MySql Data


This program is for create XML file using data taken from Mysql server. The .XML file will create in your D drive. You may change location according to your system setting and also don’t forget to change database name and table name of program.



Step 1) Copy and paste below code to create .XML file from MySql Database.
<?php
$conn=mysql_connect("localhost","root","");
if($conn==NULL)
  echo "Connection loss";
$db=mysql_select_db("ajax",$conn);
if($db==NULL)
  echo "Connection loss";
$qur="select * from stud";
$ans=mysql_query($qur);
$output.= "<root>";
while($row=mysql_fetch_array($ans))
{
  $output.="<person>";
  $output.="<name>".$row['name']."</name>";
  $output.="<surname>".$row['surname']."</surname>";
  $output.="<roll>".$row['roll']."</roll>";
  $output.="</person>";
} 
$output.="</root>";

$file_name = "D:\\myxml.xml";
$file_pointer = fopen($file_name, "w+");
fwrite($file_pointer, "$output");
fclose($file_pointer);
print "data written to file successfully"; 
?>

3 comments:

  1. hi, i tried the codes,it`s creating the xml file but the xml elements are being displayed one after another as follows(souravkayal). There elements are not being indented.can i have some help please??

    ReplyDelete
  2. *as follows (souravkayalkayal......)

    ReplyDelete