Guest convention Posted November 11, 2006 Share Posted November 11, 2006 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 https://forums.phpfreaks.com/topic/26898-how-to-use-a-while-loop-to-write-all-mysql-entries-to-a-file/ Share on other sites More sharing options...
marcus Posted November 11, 2006 Share Posted November 11, 2006 [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 " "; 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 https://forums.phpfreaks.com/topic/26898-how-to-use-a-while-loop-to-write-all-mysql-entries-to-a-file/#findComment-123006 Share on other sites More sharing options...
Guest convention Posted November 11, 2006 Share Posted November 11, 2006 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 https://forums.phpfreaks.com/topic/26898-how-to-use-a-while-loop-to-write-all-mysql-entries-to-a-file/#findComment-123029 Share on other sites More sharing options...
marcus Posted November 11, 2006 Share Posted November 11, 2006 no problem. Link to comment https://forums.phpfreaks.com/topic/26898-how-to-use-a-while-loop-to-write-all-mysql-entries-to-a-file/#findComment-123033 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.