Create your own RSS feed with PHP and Mysql

Create a RSS feed with MySQL and PHPA RSS feed is a great way of distributing your content to new and existing users. RSS feeds are a fine alternative to email newsletters, since users can read the feeds when they want, and modern feed readers givers the users great overview over many feed sources.

In this post you can learn how easy it is to create a RSS feed with PHP and MySQL

Creating the RSS feed with PHP and MySQL

The script below assumes that you have a MySQL database that has the following content for the RSS feed  in a table:

  • Headline
  • Content
  • Url (permalink)

You can insert whatever SQL query in the $result parameter that selects the content that you want to include in your RSS feed.

The following SQL can be customized to select the 10 latest posts:

SELECT * FROM [table "0" not found /]
ORDER BY  id desc limit 10

The rest of the code is pretty straight forward:


<?

// create rss content

[Insert your MySQL connection function here]

$result = mysql_query("[Insert your MySQL select statement here]");

$date = date("D, j M Y G:i:s O");

header('Content-type: text/xml');

echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
echo ("<rss version=\"2.0\">\n");
echo ("<channel>\n");
echo ("<lastBuildDate>$date</lastBuildDate>\n");
echo ("<pubDate>$date</pubDate>\n");
echo ("<title>[Insert your feed title here]</title>\n");
echo ("<description><![CDATA[[Insert feed description here]]]></description>\n");
echo ("<link>[Insert feed url here]</link>\n");
echo ("<language>[Insert feed language here]</language>\n");
echo ("<managingEditor>[Insert editor email here]</managingEditor>\n");
echo ("<webMaster>[Insert webmaster email here]</webMaster>\n");

$ch=100;
while ($data = mysql_fetch_array($result)) {
$headline       = $data["headline"];
$posturl         = $data["posturl"];
$content         = $data["content"];

echo ("<item>\n");
echo ("<title>$headline</title>\n");
echo ("<link>$posturl</link>\n");
echo ("<description><![CDATA[$content]]></description>\n");
echo ("<guid isPermaLink=\"true\">$posturl</guid>\n");
echo ("</item>\n");
}
echo ("</channel>\n");
echo ("</rss>\n");
?>

When you’re finished with the script, you should ensure that the script validates in the W3C Feed Validation Service

Last but not least you should make the feed available to your users, by placing links and RSS icons on your site.

Another marketing option is to submit the link to Feedburner. Feedburner can help distributing your feed, and provide subscriber and usage statistics.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

3 Replies to Create your own RSS feed with PHP and Mysql

  1. thalhah says:

    it only show 3 article. how to show all?

  2. Aneeq says:

    There are several ways to read RSS feed in PHP, but this one is surely one of the easiest.

    channel->item as $entry) {
    echo “link’ title=’$entry->title’>” . $entry->title . “”;
    }

    ?>

    Source:
    http://phphelp.co/2012/04/23/how-to-read-rss-feed-in-php/
    OR
    http://addr.pk/a0401

Trackbacks for this post

  1. Tweets that mention Create your own RSS feed with PHP and Mysql | Tips4PHP -- Topsy.com

Comments are now closed for this article.