Create RSS Feed Using PHP and MySQL

This tutorial is about how to Create RSS Feed Using PHP and MySQL. RSS Feed is very significant for a website to deliver latest updates to users. User able to find website's latest updates on RSS Feed and they also can find previous posts on RSS Feed. You can get regular traffic by using RSS Feed. Feed Subscribers are your website's regular visitor. You will get a lot of returning visitor by this and If you will get this then your website will reach the top of search engine day by day. Google loves returning visitors, even every search engines like that. If you get a lot of returning visitor then it profs that you provide quality contents and these is helpful for users. Google loves the quality. You can do this easily if you use RSS Feed in your website. So why late? start using php atom feed reader from now. I will assist you to make this RSS Feed using PHP and MySQL. If you use dynamic RSS Feed then you will get a lot of facilities. Your RSS Feed Atom will update automatically if you update any content on your website. So now time to learn how to create RSS feed using MySQL and PHP. I am creating a live RSS Feed in this article. This is similar to Create XML Sitemap Using PHP and MySQL. just follow my instructions step by step.

Step-1. Create Database Table For RSSFeed

If you have a running dynamic website and wants to use RSS Feed to this website then just ignore this step. You have to choose a database and create a database table for RSS Feed item. We have created a table for RSS Feed example. Here is the table structure.

link VARCHAR(255) NULL,
description TEXT DEFAULT NULL,
Install import this table into your test database and follow the next step. Insert some data into this database table so that your RSS will be live with feed item. If you do this then our first step is complete now we have to go our next step.

Step-2. Create Necessary Functions For RSS Feed

In this step, we have to create some necessary functions. We have created this before. Now create a PHP file name as rss.class.php and paste this code into this file -


class rss
public $conn;
public function __construct()
die('Failed to connect with MySQL');

public function conn()
$host = "localhost";
$user = "root";
$pass = "";
$name = "test";
$conn = mysqli_connect($host,$user,$pass,$name);
if (mysqli_connect_errno())
die("Failed to connect with MySQL: ".mysqli_connect_error());
return $this->conn = $conn;
public function head($title,$description,$siteurl)
echo '<rss xmlns:atom=""; version="2.0">';
echo '<channel>';
echo '<title>'.$title.'</title>';
echo '<link>'.$siteurl.'</link>';
echo '<description>'.$description.'</description>';
echo '<language>en-us</language>';
echo '<atom:link href="'.$siteurl.'/rss.xml" rel="self" type="application/rss+xml"/>';

public function feed($name,$url,$description,$publish)
echo '
public function foot()
echo '</channel>';
echo '</rss>';
public function clean($string) {
$string = strtolower( preg_replace('@[\W_]+@', '-', $string) );
$string = rtrim($string,'-');
$string = strtolower($string);
return $string;

public function close()

These are our necessary functions to structured the RSS. First, you should configure MySQL to connect successfully with MySQL server. You can configure it from conn() function. Change $name, $pass, $user and other necessary information's you want to change. In head() function I defined XML RSS page configurations, RSS title, RSS description, RSS link. feed() function will help us to display RSS Feed item. We completed our second step, now we will go to the next step.

Step-3. Display RSS Feed in PHP

This step is for display RSS into PHP file. Just create a PHP file name as rss.php and write down this code bellow.


header("Content-Type: application/xml; charset=UTF-8");
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
$url = 'http://'.$_SERVER['HTTP_HOST'];
include_once 'rss.class.php';
$rss = new rss;
$rssTitle = 'Latest Updates';
$rssDescription = 'Get Latest Updates Of This Site';

$query = "SELECT * FROM articles ORDER BY id";
if ($result = mysqli_query($rss->conn(), $query)) {
while ($row = mysqli_fetch_assoc($result)) {
$link = $url.slash.rss::clean($row['name']).slash;
$date= date("D, d M Y H:i:s T", $row['date']);
if (preg_match('/^.{1,500}\b/s', $row['description'], $match))
This code should display full RSS Atom from the database table. If you install this completely following this steps then check it now. This will be displayed like If you want to display it as then you should rewrite some .htaccess rule. Append these code to your htaccess file. If you want to display RSS Feed RSS.xml then append following code

RewriteEngine On
RewriteRule ^rss.xml$ rss.php [NC,L]
If you want to display your RSS Feed like as then append this following code in your website's htaccess file.
RewriteEngine On
RewriteRule ^feed/?$ rss.php [NC,L]
You will get some exclusive htaccess tips and tricks in 15 useful htaccess tips and tricks which were published in our blog before. If you do all task correctly then your job is complete. This is ready for use you can use this.

I hope you understand this. I attached full project with this article. You can download and start using instantly if you want. I hope this will be helpful for you. If you have any question or comment about this RSS Feed Using PHP and MySQL article please don't hesitate to do that, please comment your question in comment section.
Download Source Code
Unlock source code with a social share.

Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy