View Full Version : PHP and XML

01-12-2010, 11:23 PM
Hello All,
wondering if you could help me? I am using PHP to output my database results in XML. At the moment PHP outputs the XML but its still .php is there a way that PHP can output my results in xml in a seperate document that will be .xml..is this even achieveable?

here is my code;


$link = mysql_connect("localhost","root","");

$query = "SELECT ilink,image_name,description FROM gallery";

$results = mysql_query($query);

echo "<?xml version=\"1.0\"?>\n";
echo "<photos path=\"images/\">\n";
while($line = mysql_fetch_array($results)) {
echo "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
echo "</photos>\n";

Thanks in advance

01-12-2010, 11:28 PM
Instead of echoing to the screen, open a file called "photos.xml" and write to it.
You also need to form the XML properly, with proper XML tags.
A lot of information about PHP creating XML files using Google.

01-13-2010, 12:27 AM

header ("content-type: text/xml");
before any output (the top of the script is preferable). That should do it. It is possible to rewrite an xml document to a php script and do this, but I believe it should work