Akenatehm Posted November 23, 2008 Share Posted November 23, 2008 Hey Guys, I am currently looking for a way to alter my script so that on a time cycle or click of the button, it reads the information from a MySQL Database and writes and saves it into an XML file without me having to do anything (preferably). Here is the code so far: <?PHP $link = mysql_connect("localhost","username","password"); mysql_select_db("databaser"); $query = 'SELECT * FROM users'; $results = mysql_query ($query); echo "<?xml version=\"1.0\"?>\n"; echo "<user"; echo $line ["id"] . ">"; while ($line = mysql_fetch_assoc($results)) { echo "<username>" . $line["username"] . "</username>\n"; echo "<username>" . $line["username"] . "</username>\n"; } echo "</users>\n"; mysql_close($link); ?> And here is how I want it to output in the XML file: <?xml version="1.0" encoding="utf-8"?> <users> <test> <id> 1 </id> <name> test </name> <password> test </password> <email> [email protected] </email> <picture> <img src="http://url.url"> </picture> </test> <test2> <id> 2 </id> <name> test2 </name> <password> test2 </password> <email> [email protected] </email> <picture> <img src="http://url.url"> </picture> </test2> </users> Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/ Share on other sites More sharing options...
Akenatehm Posted November 23, 2008 Author Share Posted November 23, 2008 Anyone able to help at all? Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-696738 Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 im kinda lazy right now but this should help its an rss i use but its really easy to change up to just an xml file <?php header('Content-type: text/xml'); ?> <rss version="2.0"> <channel> <title>Lawrence Guide</title> <description>Lawrence guide brings you a fast and eay wau to find all the local services</description> <link>http://www.lawrenceguide.org</link> <copyright>Dropfaith Productions 2008</copyright> <?php // includes include("../template/conf.php"); // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); $q="SELECT * FROM Art ORDER BY Id DESC LIMIT 0,15"; $doGet=mysql_query($q); while($result = mysql_fetch_array($doGet)){ ?> <item> <title> <?=htmlentities(strip_tags($result['Name'])); ?></title> <description> </description> <link>http://www.lawrenceguide.org/artist/profile.php?Name=<?=$result['Name'];?></link> </item> <?php } ?> </channel> </rss> Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-696761 Share on other sites More sharing options...
Akenatehm Posted November 23, 2008 Author Share Posted November 23, 2008 Can you maybe help me out a bit more and give a better description of what everything does and what I need to change in it? I am quite new to PHP. Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-696775 Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 wow im not sure this is write i just tagged it out in a few seconds okay // lines in php are comments so all of those are explaining things as best i can (im not good at explaining code) but in reality unless i messed up somewhere all you need to edit is instead of my $host = "HOST"; $user = "USERNAME"; $pass = "PASSWORD"; $db = "DATABASE NAME"; with your details and edit the field names in this to match your mysql table <?=$result['Id'];?> id is the field i used here <item> <id> <?=$result['Id'];?> </id> <name> <?=$result['Name'];?> </name> <password><?=$result['Password'];?> </password> <email> <?=$result['Email'];?> </email> <picture> <img src="<?=$result['Imageurl'];?>"> </picture> </item> <?php header('Content-type: text/xml'); ?> <?xml version="1.0" encoding="utf-8"?> <users> <?php $host = "HOST"; $user = "USERNAME"; $pass = "PASSWORD"; $db = "DATABASE NAME"; // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // run your select from the db $q="SELECT * FROM users"; $doGet=mysql_query($q); // while loops thru the database rows and prints out the stuff in it while($result = mysql_fetch_array($doGet)){ ?> <item> <id> <?=$result['Id'];?> </id> <name> <?=$result['Name'];?> </name> <password><?=$result['Password'];?> </password> <email> <?=$result['Email'];?> </email> <picture> <img src="<?=$result['Imageurl'];?>"> </picture> </item> <?php } ?> </users> Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-696785 Share on other sites More sharing options...
Akenatehm Posted November 23, 2008 Author Share Posted November 23, 2008 Thank you very very much for your help. So will this save as an XML file? Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-696787 Share on other sites More sharing options...
dropfaith Posted November 23, 2008 Share Posted November 23, 2008 that will create a dynamic xml file from your database it wont actually save it displays any content in your db tho on a dynamic page http://lawrenceguide.org/feeds/newsfeed.php saves no content to the file but it displays from my db (thats my rss) or the file i displayed the first time.. Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-696790 Share on other sites More sharing options...
Akenatehm Posted November 24, 2008 Author Share Posted November 24, 2008 Is there any way to get this to actually save the file? Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-697382 Share on other sites More sharing options...
Akenatehm Posted November 24, 2008 Author Share Posted November 24, 2008 Hellooo? Is there any way to do this at all? Link to comment https://forums.phpfreaks.com/topic/133858-auto-write-mysql-table-data-to-xml-file/#findComment-697457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.