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.
Writing MYSQL data into an XML file using PHP Code.
Moderator: General Moderators
Welcome to the forums! The first thing anyone will tell you is to put your code in PHP tags, as I'm doing now
$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.
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++;
}
?>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.