Jump to content

Auto Write MySQL Table Data to XML File


Akenatehm

Recommended Posts

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

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>

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>

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..

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.