Create an RSS Feed with PHP and MySQL

Nitasha BatraSep 18, 2017
Create an RSS Feed with PHP and MySQL

RSS feed is best solution for getting backlinks to your website and for increasing your website traffic. In this article you'll learn how to create your own custom RSS feeds using PHP and MySQL.

We'll first learn how to create two database tables and then how to retrieve data from them which will be formatted into an RSS feed. Here's an example of the completed RSS feed and a download link to the code.


Basic structure of RSS feed

<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
	<title>Website Title </title>
	<link>Website URL</link>
	<description>Website description</description>
		<title>Item/Article Title</title>
		<link>Item/Article URL </link>
		<description>Items/Article description </description>

Create table in database

  `title` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `link` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
-- Dumping data for table `post`
INSERT INTO `post` (`id`, `title`, `link`, `description`) VALUES
(1, 'How to Build countdown timer in JavaScript', '', 'Building countdown timer in javascript is quite easy. We just have to focus on core functions of javascript i.e. setInterval, Math, Date etc.'),
(2, 'Create Web Service in Java', '', 'Web Service is a method that is used for the purpose of exchanging the data with other web application via the web or cloud using HTTP,XML, SAOP etc. A main advantage of this is that it allows for communicate data between different languages.'),
(3, 'Why Java is a secure way to go for Software development', '', 'Security is an important feature and Java’s security model is one of the key architectural features that make it most trustful choice when it comes to developing enterprise-level applications. Security becomes critical when software is downloaded across network and executed locally, and Java easily reduce the security vulnerabilities associated with the projects or applications. '),
(4, 'Wordpress Plugins to create social networking website', '', 'Social networking is a powerful tool for connecting peoples to each other. This is not easy to create social networking website like facebook and twitter but now you can create your social networking website in wordpress with functionalities like chatting, private messages, profile photo, status, Video uploading, images uploading, like, follow and more. '),
(5, 'How to generate dynamic pdf file in java application', '', 'We can create the pdf file using iText library. The iText library is supports the generation of HTML, PDF, RTF, and XML documents. It contains classes to generate the PDF file in different font, add water marks and to generate table in the PDF file ');


Files - index.php

<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to Create an RSS Feed with PHP and MySQL</title>
    <link rel="stylesheet" href="css/style.css" />
    <div class="container">
        <div class="header">
            <img src="images/eTutorialz.png" />
        </div><!-- header -->
        <h1 class="main_title">How to Create an RSS Feed with PHP and MySQL</h1>
        <div class="content">
            <a href="#[RSS File ]" class="button_rss">Click to get RSS feed</a>
        </div><!-- content -->    
        <div class="footer">
            Powered by <a href=""></a>
        </div><!-- footer -->
    </div><!-- container -->

Files - rss.php

// PDO connect *********
function connect() {
    return new PDO('mysql:host=localhost;dbname=rss_feed', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$pdo = connect();
// posts *******************************
$sql = 'SELECT * FROM post ORDER BY id DESC';
$query = $pdo->prepare($sql);
$rs_post = $query->fetchAll();
// The XML structure
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>Etutorialz : technical hub for all</title>';
$data .= '<link></link>';
$data .= '<description>Free Web tutorials with source code, PHP Tutorials, JavaScript Tutorials, HTML Tutorials and CSS Tutorials</description>';
foreach ($rs_post as $row) {
    $data .= '<item>';
    $data .= '<title>'.$row['title'].'</title>';
    $data .= '<link>'.$row['link'].'</link>';
    $data .= '<description>'.$row['description'].'</description>';
    $data .= '</item>';
$data .= '</channel>';
$data .= '</rss> ';
header('Content-Type: application/xml');
echo $data;




* {
	margin: 0;
	padding: 0;
body {
	padding: 10px;
	background: #eaeaea;
	text-align: center;
	font-family: arial;
	font-size: 12px;
	color: #333333;
.container {
	width: 1000px;
	height: auto;
	background: #ffffff;
	border: 1px solid #cccccc;
	border-radius: 10px;
	margin: auto;
	text-align: left;
.header {
	padding: 10px;
.main_title {
	background: #cccccc;
	color: #ffffff;
	padding: 10px;
	font-size: 20px;
	line-height: 20px;
.content {
	padding: 10px;
	min-height: 100px;
	text-align: center;
.footer {
	padding: 10px;
	text-align: right;
.footer a {
	color: #999999;
	text-decoration: none;
.footer a:hover {
	text-decoration: underline;
.button_rss {
	width: 200px;
	height: 30px;
	display: block;
	text-align: center;
	background: #a3a3a3;
	border: 1px solid #808080;
	color: #ffffff;
	border-radius: 10px;
	cursor: pointer;
	margin: auto;
	margin-top: 20px;
	font-size: 14px;
	line-height: 30px;
	font-weight: bold;
	text-decoration: none;


As above explained steps, you can easily create RSS by using PHP and Mysql. Here we have also shared zip file of code of this article. You can download this code and implement with your website.