Tutorials: How to write a PHP & MySQL RSS Feed Generator
RSS (Really Simple Syndication) is a great way of getting your blog/articles to a wider audience and notify them of new posts. This tutorial will explain how to write your own PHP script to read your content dynamically from a MySQL database and write a valid RSS feed.
First things first
Start a new PHP file - this will be your generator, which will obtain the content from the database and parse it into an XML format.
Firstly, let's set some variables for your RSS feed:
$rss_site= "yoursite.com" ;
$rss_description= "A brief description of your site's RSS feed, about this long";
$rss_language="en";
$rss_logo="http://www.".$rss_site."/images/rss.gif";
$emailadmin="whatever@youremailis.com";
That sets up some basic variables for the RSS feed. You don't need an RSS_logo (an image associated with your feed, but it is a nice touch). NOTE: if you don't wish to have a logo associated with your feed, you will need to modify the references to the image later on.
Some kind advice - while it is useful to have an email address associated with the feed, it is advisable to have an address specially for this - because it will be in for a whole world of spam.
Setting the header
This is VERY important, this sets the output header to be recognised as XML. Include this code after your above variables.
Connect to your Database
Here we bung in the normal PHP stuff to tell the script where to look for your articles/blogs, and secondly, define an SQL query to obtain your desired content.
@mysql_select_db("database-name") or die("Unable to select DB");
With the database connection established, we need to define a query (to select the content you want your RSS feed to present).
$result = mysql_query($query) or die("Query failed") ;
The above statement obtains all the latest Articles from our fictional database, ordering them by their ID in descending order (which should present them in a 'newest-first' manner). You can order it by a created-date if you so wish - it's basically to ensure your feed is supplying the newest content first.
