Generate XML from mysql table using PHP

Its very easy generating xml from php. I have created the following script to achieve this. The same script I used to create simple query analyser using flex.

<?
header(“content-type:text/xml”);
function getXML($sql=”Default Query”)
{
$conn=mysql_connect(“hostName”,”userName”,”password”);
$db=mysql_select_db(“databaseName”);
$result = mysql_query($sql,$conn);
$columns=””;
echo “<records>”;
while($row=mysql_fetch_assoc($result))
{
$columns.=”<record>”;
foreach($row as $key => $value)
{
$columns.=”<$key>$value</$key>”;
}
$columns.=”</record>”;
}
echo $columns;
echo “</records>”;
}
getXML(“SELECT * FROM tableName”);

?>

This simple function is very useful in different manner. You can used it when you want to write ajax application, Flex Application.

Advertisements

5 Responses to Generate XML from mysql table using PHP

  1. A great start to what I want to build – a Flex front end for wordpress for a blog that is actually more of a product gallery but using the category / post editing features of WordPress as it’s CMS. Just have to figure out the post = picture piece and pull it together. Thanks for sharing!

  2. Baalu says:

    thanks sir. its very use full for me ..

    Thanks once again..

    |)
    |) (\ (\ |_ |_|

  3. Varun says:

    Thanks a lot!!!

    I needed it for my project.

  4. Charlie says:

    Thankyou so much, i really needed this, I didn’t think about content-type headers, so my mp3 player wouldn’t work for ages, thanks!

  5. raghavendra says:

    Hi , i have data like in mysql say column name and value but i need this data to be displayed in this formate…

    could you help in on this please

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: