CW Web Solutions Logo
"Web Design, Web Development, Security and Internet Marketing"
Tutorials
Tutorials

Export mysql database table data to xml file

Thursday, July 9, 2009

Requirements: write access to folder XML file will be stored

<?php
// STORE DATABASE VARIABLES
$hostname_cnConnection = "website.com";
$database_cnConnection = "databasename";
$username_cnConnection = "username";
$password_cnConnection = "password";
$cnConnection = mysql_pconnect($hostname_cnConnection, $username_cnConnection, $password_cnConnection);

// CONNECT TO DATABASE
mysql_select_db($database_cnConnection, $cnConnection);
$query_rsRecordset = "SELECT * FROM tablename";
$rsRecordset = mysql_query($query_rsRecordset, $cnConnection) or die(mysql_error());
$row_rsRecordset = mysql_fetch_assoc($rsRecordset);
$totalRows_rsRecordset = mysql_num_rows($rsRecordset);

// START STORING DATA IN VARIABLE TO PLACE IN XML FILE
if($totalRows_rsRecordset > 0) {

$strXML = "<?xml version=\"1.0\"?>\n";
// STORE NAME OF TABLE
$strXML = $strXML . "<tablename>\n";
// STORE FIELD AND FIELD DATA IN ONE HIARCHY, REPEAT FOR MULTIPLE FIELDS
$strXML = $strXML . "   <fieldname title=\"PrimaryID\">\n";
do {

$strXML = $strXML . "      <id>" . $row_rsRecordset['fieldname'] . "</id>\n";

} while ($row_rsRecordset = mysql_fetch_assoc($rsRecordset));
$strXML = $strXML . "</tablename>";
$strXML = $strXML . "   </fieldname>\n";
// OPEN FILE, WRITE TO FILE, CLOSE FILE, CLOSE RECORDSET
$XMLFile = fopen("myxmlfile.xml", "w");
fwrite($XMLFile, $strXML);
fclose($XMLFile);

}
mysql_free_result($rsRecordset);
?>

Bookmark and Share
Comments
You can make comments without registration. All comments are moderated. Click the Comment link at bottom of the page to open the comment form.
Tuesday, Nov 17, 2009 9:00am EST
tom
"how do I get around the permission issue at this line: $XMLFile = fopen("myxmlfile.xml", "w");"
Reply to Comment
Saturday, Feb 13, 2010 6:22pm EST
CW Web Solutions
"Set the XML file permissions to 644 or 755."
Reply to Comment
Make a Comment
CW Web Solutions LLC, an Atlanta based company
2009 Copyright Pending