Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an XML file via PHP / MySQL

Status
Not open for further replies.

dreamaz

Technical User
Dec 18, 2002
184
CA
Hi,

I have a php.php file which fetches data from a mysql table and displays the results in XML but within the php.php page

I would like to results to be outputed to an XML file instead... is this possible? I currently have the results in variable $xml_output and then echoing it on the page.. this works fine so far, but need the results in an XML file. Here's what i have so far:

<?php

header("Content-type: text/xml");

$host = "localhost";
$user = "test";
$pass = "test";
$database = "education";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM blog ORDER BY title DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<entries>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<entry>\n";
$xml_output .= "\t\t<title>" . $row['title'] . "</title>\n";
// Escaping illegal characters
$row['objective'] = str_replace("&", "&", $row['objective']);
$row['objective'] = str_replace("<", "<", $row['objective']);
$row['objective'] = str_replace(">", "&gt;", $row['objective']);
$row['objective'] = str_replace("\"", "&quot;", $row['objective']);
$xml_output .= "\t\t<objective>" . $row['objective'] . "</objective>\n";
$xml_output .= "\t</entry>\n";
}

$xml_output .= "</entries>";

echo $xml_output;

?>


Any help is appreciated.

dR
 
Where should this XML file be?

If you mean you want to create an XML file on the web server's filesystem, I recommend that you use fopen() and fwrite() to write data from the variable to the file?

If you meant you want to stream the data to a browser and have the browser offer the user to save the file, then I recommend you take a look at the PHP online documentation on header(), reading the last example code near the bottom of the article (before the user-supplied comments. Look for "content-disposition" on the page).



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top