Jump to content

How to use a while loop to write all MySQL entries to a file?


Guest convention

Recommended Posts

Guest convention
Hello,

I set up a script to write data to a XML file, but even though I used a while loop to access the MySQL database entries, it only writes the first database entry to the file. I also put the while loop in a function, but that did not work. I also put the whole thing in the fwrite function itself (instead of having it in a variable), which only wrote the text 'Resource id #2' (or something similar) to the file.

[code]
mysql_connect("localhost","root","root");
mysql_select_db("dbname");
$query = mysql_query("SELECT * FROM tablename");

$fi = "test.xml";
while ($row = mysql_fetch_array($query)) {
$st =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<rss version=\"2.0\">
<channel>
  <title>Test XML File</title>
  <link>yeah</link>
  <description>yeah</description>
  <item>
   <title>".$row['title']."</title>
   <link>".$row['url']."</link>
   <description>Added on ".$row['date']." at ".$row['time'].".</description>
  </item>
</channel>
</rss>
";
}

$fo = fopen($fi, 'w') or die ("fopen error");
fwrite($fo, $st) or die ("fwrite error");
fclose($fo) or die ("fclose error");
[/code]

How could I write all of the database entries (as seperate XML items) to the file?
Link to comment
Share on other sites

[code]
mysql_connect("localhost","root","root");
mysql_select_db("dbname");
$query = mysql_query("SELECT * FROM tablename");

$fi = "test.xml";
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<rss version=\"2.0\">
<channel>";
while ($row1 = mysql_fetch_array($query, MYSQL_BOTH)) {
    echo "&nbsp;";
    printf ("
  <title>Test XML File</title>
  <link>yeah</link>
  <description>yeah</description>
  <item>
  <title>%s</title>
  <link>%s</link>
  <description>Added on %s at %s.</description>
  </item>",$row1[title],$row1[url],$row1[date],$row1[time]);
};
mysql_free_result($query);
echo "</channel>
</rss>";
[/code]

you can try that
Link to comment
Share on other sites

Guest convention
Thanks for the help, but I just realized to do this:

[code]
$fi = "test.xml";
$st = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<rss version=\"2.0\">
<channel>
  <title>test</title>
  <link>test</link>
  <description>test</description>
";
while ($row = mysql_fetch_array($query)) {
$st .=  "
  <item>
    <title>".$row['title']."</title>
    <link>".$row['url']."</link>
    <description>Added on ".$row['date']." at ".$row['time']."</description>
  </item>
";
}
$st .= "
</channel>
</rss>
";
[/code]

And it worked perfectly. Thanks again for the help. :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.