cmaclennan Posted June 7, 2009 Share Posted June 7, 2009 Hi Guys, I have a file that I use to pull data from a mysql database and im looking for some help in being able to execute it to be able to save an xml file manually without having to do that part manually. Can anyone offer some insight on how i may go about this i've heard mention using the fput function just not sure how this would work. Thanks in advance. <?php //Developed by -==[Mihir Shah]==- during my Project work //for the output header("Content-type: text/xml"); //to create connection to database $connection = mysql_connect("user","pass", "db") or die ("could not connect to database"); //to select the database here test is the sample database come with mysql $db = mysql_select_db("powercart",$connection) or die ("Couldn't select database."); $rs = mysql_query("SELECT * FROM customers",$connection) or die ("invalid query"); //count the no. of columns in the table $fcount = mysql_num_fields($rs); //you can choose any name for the starting tag echo ("<customers>"); while($row = mysql_fetch_array( $rs ) ) { echo ("<customer>"); for($i=0; $i< $fcount; $i++) { $tag = mysql_field_name( $rs, $i ); echo ("<$tag>". $row[$i]. "</$tag>"); } echo ("</customer>"); } echo ("</customers>"); ?> Here's what i have that generates the xml in the browser and it works great just trying to automate it a bit. Quote Link to comment Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Share Posted June 7, 2009 Try writing the contents to a file by using the fwrite function: $file = fopen("MyXML.xml", "w"); //File is created if it doesn't exist already, and it is overwritten if it does. fwrite($file, $myXML); //$ Outputs the contents of $myXML to the MyXML.xml file. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted June 7, 2009 Author Share Posted June 7, 2009 did i miss something or do something wrong i added the code you proovided and got this error: Only one top level element is allowed in an XML document. Error processing resource 'http://solidweb/powercart/cust_xml.php... Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted June 7, 2009 Author Share Posted June 7, 2009 Ok so I got rid of the error but instead of writing the xml data to the file it seems have only put "Resource id #3" and thats it?? Quote Link to comment Share on other sites More sharing options...
kobmat Posted June 7, 2009 Share Posted June 7, 2009 This generates the xml and echos it the browser <?php ob_start(); //Developed by -==[Mihir Shah]==- during my Project work //for the output header("Content-type: text/xml"); //to create connection to database $connection = mysql_connect("user","pass", "db") or die ("could not connect to database"); //to select the database here test is the sample database come with mysql $db = mysql_select_db("powercart",$connection) or die ("Couldn't select database."); $rs = mysql_query("SELECT * FROM customers",$connection) or die ("invalid query"); //count the no. of columns in the table $fcount = mysql_num_fields($rs); $h = ob_get_clean(); //you can choose any name for the starting tag echo ("<customers>"); while($row = mysql_fetch_array( $rs ) ) { echo ("<customer>"); for($i=0; $i< $fcount; $i++) { $tag = mysql_field_name( $rs, $i ); echo ("<$tag>". $row[$i]. "</$tag>"); } echo ("</customer>"); } echo ("</customers>"); $xml = ob_get_clean(); $fp = fopen("document.xml", "w"); if($fp) { fwrite($fp, $xml); } echo $h.$xml ?> or this <?php //Developed by -==[Mihir Shah]==- during my Project work //for the output header("Content-type: text/xml"); //to create connection to database $connection = mysql_connect("user","pass", "db") or die ("could not connect to database"); //to select the database here test is the sample database come with mysql $db = mysql_select_db("powercart",$connection) or die ("Couldn't select database."); $rs = mysql_query("SELECT * FROM customers",$connection) or die ("invalid query"); //count the no. of columns in the table $fcount = mysql_num_fields($rs); //you can choose any name for the starting tag $xml = "<customers>"; while($row = mysql_fetch_array( $rs ) ) { $xml .= "<customer>"; for($i=0; $i< $fcount; $i++) { $tag = mysql_field_name( $rs, $i ); $xml .= "<$tag>". $row[$i]. "</$tag>"; } $xml .= "</customer>"; } $xml .= "</customers>"; $fp = fopen("document.xml", "w"); if($fp) { fwrite($fp, $xml); } //remove this if you dont need to output to the browser echo $xml; ?> Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted June 7, 2009 Author Share Posted June 7, 2009 still only showing "Resource id #3" in the file, am i missing something? Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted June 7, 2009 Author Share Posted June 7, 2009 I'm also back to getting the "Only one top level element" error which shows up near the bottom of the page for some reason. Since the page is short here it is. <?php //for the output header("Content-type: text/xml"); //to create connection to database $connection = mysql_connect("localhost","powercart", "powercart") or die ("could not connect to database"); //to select the database here test is the sample database come with mysql $db = mysql_select_db("powercart",$connection) or die ("Couldn't select database."); $rs = mysql_query("SELECT * FROM customers",$connection) or die ("invalid query"); //count the no. of columns in the table $fcount = mysql_num_fields($rs); //you can choose any name for the starting tag echo ("<customers>"); while($row = mysql_fetch_array( $rs ) ) { echo ("<customer>"); for($i=0; $i< $fcount; $i++) { $tag = mysql_field_name( $rs, $i ); echo ("<$tag>". $row[$i]. "</$tag>"); } echo ("</customer>"); } echo ("</customers>"); $fp = fopen("document.xml", "w"); if($fp) { fwrite($fp, $xml); } //remove this if you dont need to output to the browser echo $xml; ?> Quote Link to comment Share on other sites More sharing options...
kobmat Posted June 7, 2009 Share Posted June 7, 2009 change echo ("<customers>"); while($row = mysql_fetch_array( $rs ) ) { echo ("<customer>"); for($i=0; $i< $fcount; $i++) { $tag = mysql_field_name( $rs, $i ); echo ("<$tag>". $row[$i]. "</$tag>"); } echo ("</customer>"); } echo ("</customers>"); to $xml = "<customers>"; while($row = mysql_fetch_array( $rs ) ) { $xml .= "<customer>"; for($i=0; $i< $fcount; $i++) { $tag = mysql_field_name( $rs, $i ); $xml .= "<$tag>". $row[$i]. "</$tag>"; } $xml .= "</customer>"; } $xml .= "</customers>"; Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted June 7, 2009 Author Share Posted June 7, 2009 Thank you very much that worked a charm. Much Appreciated Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.