Hello Experts today i will discus about a XML write function and this XML value come from mysql table. Its very simple for use. Here is a sample mysql table and steps are described. I think it will be helpful for Experts…
<?php
/**
* writeXML : Converting mysql table into XML
*
* @author : S.M. Saidur Rahman ,
* Moderator, joomla_experts (http://tech.groups.yahoo.com/group/joomla_experts/)
Moderator, cakephpexperts (http://tech.groups.yahoo.com/group/cakephpexperts/)
* URL: http://ranawd.wordpress.com/
* @version : 1.0
* @date 2008-07-09
* Purpose : Write XML file and collect data from mysql table
*/
/*
@Steps:
#Create a table name “sampletable”
#Create a XML file name “sample.xml”
#Correct root path as define value
#Call this function and Enjoy!
*/
//Here is an example of mysql table
CREATE TABLE sampletable
(
id
int(11) NOT NULL auto_increment,
image
varchar(255) collate latin1_general_ci NOT NULL,
thumbnail
varchar(255) collate latin1_general_ci NOT NULL,
caption
varchar(150) collate latin1_general_ci NOT NULL,
ordering
int(11) NOT NULL default ‘0’,
PRIMARY KEY (id
)
)
//Define XML file root path
define(‘ROOT_PATH’, $_SERVER[‘DOCUMENT_ROOT’]);
//XML write function
function writeXML($xmlfile, $tablename)
{
$xmlfile_path= ROOT_PATH.”/”.$xmlfile;
$query_xmlw = “SELECT * FROM $tablename ORDER BY ordering DESC”;
$result_xmlw = mysql_query($query_xmlw);
$xmlstore=”;
$xmlstore .=”;
while($row_xmlw = mysql_fetch_array($result_xmlw)) {
$xmlstore .=”.$row_xmlw[‘image’].”;
$xmlstore .=”.$row_xmlw[‘thumbnail’].”;
$xmlstore .=’
‘;
$xmlstore .=”;
}
$xmlstore .=”;
$handle = fopen($xmlfile_path, ‘w’);
fwrite($handle, $xmlstore);
}
//Put XML file name and mysql table name simultaniously
writeXML(‘sample.xml’, ‘sampletable’);
?>
www.redips.net | From MySQL to XML with PHP
Hi!
Just to add another PHP generator. I tried to wrote as simple as possible PHP function which will be able to produce hierarchical XML from the MySQL. My sql2xml function is several years in production, so it is well tested. I hope you will find useful informations.
Thank you for your post.