Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building an XML file.

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
Hi

I have a PHP script which connects to my (mySQL) database, pulls out a list of items from the database and then displays them on the page. Easy peasy!

What I want to do now is pull the data from the database and create a XML file.

I have a developer who has built a Flash page for me and he wants the data to populate the page in an XML file.
Is there a good tutorial somewhere I could read that would teach me how to create this XML file from my script?

~D
 

Code:
<?php 
//XML output of an existing MySql database 
[b]header("Content-type: text/xml"); [/b]

//to create connection to database 
$connection = mysql_connect("127.0.0.1","username", "password") 
or die ("could not connect to database"); 

//to select the database here test is the sample database come with mysql 
$db = mysql_select_db("test",$connection) 
or die ("Couldn't select database."); 

$rs = mysql_query("select * from tablename",$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 ("<result>"); 
while($row = mysql_fetch_array( $rs ) ) 
{ 
echo ("<tablerow>"); 
for($i=0; $i< $fcount; $i++) 
{ 
$tag = mysql_field_name( $rs, $i ); 
echo ("<$tag>". $row[$i]. "</$tag>"); 
} 
echo ("</tablerow>"); 
} 
echo ("</result>"); 
?>

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top