Writing MYSQL data into an XML file using PHP Code.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rizen
Forum Newbie
Posts: 3
Joined: Thu Jun 24, 2004 3:59 pm

Writing MYSQL data into an XML file using PHP Code.

Post by rizen »

Hello,

I am a newbie to PHP code.

I have a mysql database and I am able to output the information into a dynamic table. What I would like to do is also creat an XML file using the same data from the mysql database.

Here is the part of the code that I am using to attempt to create and write to an XML file.

<?php
$xmldec = '<?xml version="1.0" encoding="iso-8859-1"?>';
touch("news.xml");
$fp = fopen("news.xml", "w");
fwrite ($fp, "$xmldec \n");
$counter = 1;
while ($counter <= $totalRows_news_records){
fwrite ($fp, "<news>\n");
fwrite ($fp, $row_news_records['id']);
fwrite ($fp, "</news>\n");
$counter++;
}
?>

The XML file gets created, and it writes the xml declaraction and the "news" tags as well. However, the records are not ouputted.

Does anyone have any suggestions?

Thanks.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Welcome to the forums! The first thing anyone will tell you is to put your code in PHP tags, as I'm doing now :)

Code: Select all

<?php
$xmldec = '<?xml version="1.0" encoding="iso-8859-1"?>';
touch("news.xml");
$fp = fopen("news.xml", "w");
fwrite ($fp, "$xmldec \n");
$counter = 1;

while ($counter <= $totalRows_news_records)
{
   fwrite ($fp, "<news>\n");
   fwrite ($fp, $row_news_records['id']);
   fwrite ($fp, "</news>\n");
   $counter++;
}
?>
$row_news_records isn't defined in this code, so that may be why it's not being output. If it's defined earlier in the file this snippet is from, then I'm not sure.

You're not looping though anything either, so if $row_news_records IS defined, it'll be the same value every time.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply