How to Create RSS feeds using PHP and MySQL-Feeds driven from MySQL database

This article is for all those web developers and bloggers who are running their blog on a custom Content management System and want to drive traffic to their blog through user email subscriptions, when we think of blog subscription we immediately think of sending every post update of our blog to our subscribers and RSS feed is one of the technology which helps in solving out this problem.As platforms like blogger, WordPress etc provide ready-made RSS subscription widgets to their user but if you are running a custom blog which is powered by PHP and MySQL then this tutorial is for you.

rss feeds using php

In this tutorial, we will make custom RSS feeds which will be driven from our custom blog’s database, let us assume that Sam have a MySQL database called articles which consist of table ‘data’ having columns, Download the source files for this tutorial from the given link Download
1-id (Unique article id)
4-Link (For SEO friendly URL, learn more about SEO friendly URL at link)
From the above columns, we will only use ‘Title’, ‘Link’ and ‘description’.
So first of all before getting our hands dirty in the code we will understand what actually RSS feeds file consists of.
An RSS feeds file is an XML file which consists of a particular type of data which can be used in different ways.
Basic structure of RSS is shown below

<rss version="2.0">   <channel>       <title>Your blog title</title>       <link></link>       <description>your blog meta description</description>       <item>           <title></title>           <link></link>           <description></description>       </item>       <item>           <title></title>           <link></link>           <description></description>       </item>   </channel> </rss>

In the above structure, only the item tags will be dynamic as all our database data is going to be stored in it.
So let’s start our coding, first of all, we will make file config.php which will consist the code which will connect to our database.


<?php $host = 'localhost'; $db = 'article'; $pwd = 'db_password';//replace with your db password $user = 'username';//replace with your db username $conn = new mysqli($host, $user, $pwd, $db) or die('cannot connect'); ?>

Now we will create our RSS.php file, to display the content as an XML file we will use the PHP header: header(‘Content-Type: application/xml’);


<?php header('Content-Type: application/xml'); ?> <rss version="2.0">   <channel>       <title>Your blog title</title>       <link>/</link>       <description>Psycho-CODES is your daily guide to computer programming and computer science.</description> <?php include "config.php"; $sql = "SELECT * FROM ORDER BY id DESC"; $result = $conn->query($sql) or die("Cannot write"); while($row = $result->fetch_assoc()){ ?> <item>           <title><?php echo $row['Title']; ?></title>           <link><?php echo $row['Link']; ?></link> //this link will be changed according to your post's URL           <description><?php echo $row['description']; ?></description> </item> <?php } ?> </channel> </rss> 

In the above code, we have used a simple RSS file structure in which the item tags are driving data from our MySQL database.
Now we have successfully created our RSS.php now the question arises how to use it, to use it first of all upload the RSS.php to your web hosting account and go to

Now enter your feed URL in the input box, for example, your feeds URL will be ‘’ and follow the steps to burn your RSS feeds.

Thank you for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий