getButterfly Logo getButterfly code wrangling since 2005

I’ll be straying a bit from WordPress to static PHP web site owners.

Sometimes you need an RSS feed to provide your users if you have a static news site powered by PHP/MySQL. Sometimes you just need it in order to promote your site, or even to automatically post to Twitter or Facebook.

What do you do if you need an RSS feed but don’t want to make the big step towards WordPress? Well, you write your own script:

[php]<?php echo'<?xml version=”1.0″ encoding=”UTF-8″?>’.”\n”;?>
<rss version=”2.0″

<title>My New RSS Feed</title>
<atom:link href=”” rel=”self” type=”application/rss+xml” />
<description>News from my new RSS feed</description>


$result = mysql_query(“SELECT * FROM news ORDER BY id DESC”) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo ‘
<guid isPermaLink=”true”>’.$row[‘id’].'</guid>
<description>’.str_replace(“&”,”and”,trim(substr(strip_tags($row[‘content’]),0,225))).’… &lt;a href=”’.$row[‘id’].'”&gt;Read More!&lt;/a&gt;</description>
<pubDate>’.date(‘D, d M y H:i:s O’, strtotime($row[‘thedate’])).'</pubDate>

Let’s break it down. The first section is pretty self-explanatory, as you need to include a special RSS 2.0 header, and RSS feed details, such as title, link, description and language.

You should then include your database connection files (you may have it named db.php, config.php or connect.php) in order to execute the query. Notice how I have my own news table which I extract data from.

Copy the code above in a rss2.php file and call it in your browser. Or use it to ping a RSS feed service.

That’s it.

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a reply