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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql to xml in php 1

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
I would like to convert a mysql database to xml. I want to display a table of customer details for members to download. Could someone direct me to a tutorial, or help as I could find none of any value. I know very little about this. Thanks
 
xml is just structured text (albeit in a strict format). just use php to select the records and fields you want, iterate through the recordset and dump out text to a file in the same way that you would dump text to a browser (remembering, of course, to put the right tags around each record and field).


 
Sorry for being slow, but could you give an example for me? Thank you for your understanding
 
let's assume your table for users is
id first_name last_name user_id

you'd do something like this

Code:
<?
$xmlfile = "";//file name
$fh = fopen ($xmlfile ,"wb") or die ("cant create file");

$sql = "Select id, first_name, last_name, user_id from usertable";

$result = @mysql_query($sql) or die (mysql_error());
while ($row[] = mysql_fetch_assoc($result)):
//do nothing
endwhile;
$lineterminate = chr(13) .chr(10);

fwrite ($fh, "<?xml version="1.0"?>$lineterminate<users>");
foreach ($row as $val):
extract($val);
$line = "
<user> $lineterminate
<id> $lineterminate
$id $lineterminate
</id> $lineterminate
<first_name> $lineterminate
$first_name $lineterminate
</first_name> $lineterminate
<last_name> $lineterminate
$last_name $lineterminate
</last_name> $lineterminate
</user> $lineterminate
";
fwrite($fh, $line);
endforeach;
fwrite ($fh, "</users>");
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top