Hi.

I need to create an RSS file from one table in my database. I'm using PHP5 and MySQL.

I have searched on this and tried to do it myself but i'm not sure what path to take. Should I use SimpleXML to do it?

I am hoping someone might be able to point me in the right general direction. So anyone done this before and can help me out?


Many thanks.

Recommended Answers

All 4 Replies

Hi RichAppsConsult,

Thanks for the page. I had actually come across that one when searching. I didn't use it as there seems to be bits that I don't need in it and I don't enderstand everything it uses such as the '<rdf:Seq>' namespace use.

I'm looking to just output 4 or 5 text fields from my DB (story_title, story, link, link_title, date) to a simple XML (in RSS format) file.

I don't need images etc and would love to keep it simple at first and just make sure I understand everything.

I see lots of stuff for parsing XML files and I can actually already do that OK but it is not as easy to find something coherent on the issue of outputting XML.

Since the XML output for an RSS is fairly static, I use smarty to generate it, instead of an XML parser.

Hi,

Thanks for the two replies.

I just got it working as the second comment came in. I thought would post up the code for others.

Basically it connects to a table and keeps adding text to the $xml_output variable and then writes what is in the variable to an xml file.

In it's current state I am still trying to get it to validate but it worked for me so should get you going.

<?php
$host = "localhost";
$user = "username";
$pass = "password";
$database = "name of DB";

$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 updates ORDER BY id DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml_output .= "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
$xml_output .= "<channel>\n";
$xml_output .= "<title>Title of whole feed</title>\n";
$xml_output .= "<link>link for whole feed</link>\n";
$xml_output .= "<description>Description of whole feed</description>\n";
$xml_output .= "<language>en</language>\n";

for($x = 0; $x < mysql_num_rows($resultID); $x++){
    $row = mysql_fetch_assoc($resultID);
    $xml_output .= "\t<item>\n";
    $xml_output .= "\t\t<title>" . $row['title'] . "</title>\n";
    $xml_output .= "\t\t<link>" . $row['link_dest'] . "</link>\n";
    $xml_output .= "\t\t<description>" . $row['description'] . "</description>\n";
	$xml_output .= "\t\t<guid isPermaLink=\"true\">http://www.yoursite.com#".$row['id']."</guid>\n";
    $xml_output .= "\t</item>\n";
}

$xml_output .= "</channel>\n";
$xml_output .= "</rss>";

$filename_path = '../feeds/filename_'.$_COOKIE['lang'].'.xml';

$file_write = file_put_contents($filename_path, $xml_output);



?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.