Recent Postings

Recent Comments


RSS2 feed


Most Popular Posts

Recent Posting

Creating an RSS Feed using Php and MySQL

Saturday, 10th February 2007 12:44am
In this post, I will illustrate Php code that can be used to create an RSS feed containing the required channel elements plus pubDate and guid using a MySQL database. This code assumes Php 5.* .

I have a used a table called post where all the posts are stored. This is a simple table with fields postid, heading, description and timestamp where heading is effectively the title and timestamp is the time of inserting the post in the database.

The class Db is a small class that handles the database side and connects to the MySQL database through the class constructor. The GetFeedData function retrieves feed data and store the data in an array. The FormatDate function formats the date to a format which includes the GMT time difference.

The Feed Class is involved with the creation of the feed.xml file. The CreateFeed function calls three functions which create the head, the body and the footer of the RSS feed. I think putting in three functions makes it easy if you want to add exception handling around the file creation process.

The last file or class not shown will just call the CreateFeed function to start the process. The CreateFeed can be called after inserting data into the post database, say using a form to create the RSS feed.

The code and the table can be expanded upon to include other optional channel elements like copyright, image etc.

delicious delicious | digg digg

Category: Php | Comments : 38