Create XML from MySQL database using PHP

1 post Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

If you use scripts or your vb project uses XML files. Then here is the easiest way to copy your database to a XML file.

This does work...I have tested on my up and coming new project.
I use this script to create a XML file on the server. Then my project makes a webrequest for this XML file.
And in populates a LIstBox, or ListView, or a TreeView.
The uses are unlimited.
Code: Select all
<?php 
$host = "HOST"; 
$user = "USERNAME"; 
$pass = "USER_PASSWORD"; 
$database = "DATABASE"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 

$query = "SELECT * FROM TABLE_NAME ORDER BY FEILD_NAME ASC"; 
$resultID = mysql_query($query, $linkID) or die("Data not found."); 

$xml_output = "<?xml version=\"1.0\"?>\r\r\n"; 
$xml_output .= "<entries>\r\r\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 
    $xml_output .= "\t<entry>\r\r\n"; 
    $xml_output .= "\t\t<pagename>" . $row['pagename'] . "</pagename>\r\r\n"; 
    $xml_output .= "\t</entry>\r\r\n"; 
} 

$xml_output .= "</entries>\n"; 

//Optional
//echo $xml_output; 

$filenamepath =   "MadedList.xml"; 
$fp = fopen($filenamepath,'w'); 
$write = fwrite($fp,$xml_output); 

?>
When this script is executed it will create a file named: MadedList.xml
Inside that XML file you will see.
Code: Select all
<?xml version="1.0"?>
<entries>
	<entry>
		<pagename>FEILD_DATA</pagename>
	</entry>
	<entry>
		<pagename>FEILD_DATA</pagename>
	</entry>
</entries>

Very simple...very easy.
Image
1 post Page 1 of 1
Return to “Tutorials”