Generate RSS Feeds from MySQL Data

Recently, I had a request from a client to create an RSS feed of its member's news. I was in luck when I remembered that the members were already entering in their own member news from their members-only area. In addition to adding contact info, I gave them the ability to enter in news and updates about their businesses, and I created a page for site visitors to review the news in one location. I felt like it would be great if I could harness this MySQL generated news list as an RSS feed. In order to proceed, I needed to create an RSS feed with the data from the rows in the database. I took my code that created the page, and slightly modified it.

// Connect to your MySQL Database
include '/path/to/your/database/connection';

// Select the data
$select = mysql_query( "SELECT * FROM your_table_name ORDER BY businessname" )
or die("SELECT Error: ".mysql_error());

// Start to build the output that will make the RSS file on the fly
$rssData = '<?xml version="1.0" encoding="UTF-8" ?>n';
$rssData .= '<rss version="0.91">';
$rssData .= '<channel>";
$rssData .= '<title>Your RSS Feed</title>';
$rssData .= '<link></link>';
$rssData .= '<description>This is a feed of our hints</description>';
$rssData .= '<language>en-us</language>';

// Go and get the rows in the database and output the Business name and News item
while ($row = mysql_fetch_array($select)) {

        // Begin the <item> tag
        $rssData .= "<item>n"; 

        // Prints the business name as the title of the RSS Entry
        $rssData .= "<title>" . $row['businessname'] . "</title>n";                         

        // Print the news item as the description of the RSS entry
        $rssData .= "<description>" . $row['news'] . "</description>n"; 

    // Close the <item> tag
    $rssData .= "</item>n";       


// Close the output and create an xml file from the output
$rssData .= "</channel>";
$rssData .= "</rss>";
Header("Content-Type: application/xml");
echo $rssData;

The exact implementation I have used takes into account that not all businesses may have entered News & Events into the fields on their member profiles. To take this into consideration, I have used the following code to eliminate businesses from the list of full News & Events with empty columns.
while ($row = mysql_fetch_array($select)) {

if ($row ['news']!= "") { // If the column has no data, its results are omitted from the RSS Feed

    $rssData .= "<item>n"; 

                $rssData .= "<title>" . $row['businessname'] . "</title>n";
                $rssData .= "<description>" . $row['news'] . "</description>n"; 

    $rssData .= "</item>n";       


The last step is to save this file as "yournews.php" and create your RSS tag in your HTML header like so...
<link rel="alternate" type="application/rss+xml" title="" href="http://your_domain/yournews.php" />
Post a comment
  1. steven davies

    hi i was wondering how to create the html file to show the rss data. I am not able to get it to work for me.

    • Matthew Price

      Hello So you can do one of two things. You can either just place all of this code into a php file and call that in the head section of your homepage, or you can have a scheduled cron job execute the php file and have it output a cache html file to your server and then use that as your rss source file. I think have understood what you are asking. Lemme know Matt

Real Time Web Analytics ^