ADVERTISEMENTS

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.

ADVERTISEMENTS

Basic structure of RSS feed

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

Create table in database

CREATE TABLE IF NOT EXISTS `post` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `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`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;
--
-- Dumping data for table `post`
--
INSERT INTO `post` (`id`, `title`, `link`, `description`) VALUES
(1, 'How to Build countdown timer in JavaScript', 'http://www.etutorialz.com/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', 'http://www.etutorialz.com/create-webservice-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', 'http://www.etutorialz.com/why-java-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', 'http://www.etutorialz.com/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', 'http://www.etutorialz.com/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>
<html>
<head>
    <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" />
</head>
<body>
    <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="http://www.etutorialz.com">etutorialz.com</a>
        </div><!-- footer -->
    </div><!-- container -->
</body>
</html>

Files - rss.php

<?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);
$query->execute();
$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>http://www.etutorialz.com</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;
?>

 

ADVERTISEMENTS

Style.css

* {
	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.

ADVERTISEMENTS