How to create an RSS Feed with PHP and Mysql - PHP Tutorial



Download Brave Browser


Generating an RSS feed for your website is very important for your SEO campaign.
When submitting your RSS feed to multiple Feed aggregators, your links get indexed much faster.

Today we will be teaching you on how to generate a feed for your website in a simple way using mysqli prepared statements and PHP.
Your new RSS feed will validate as a good feed here:
https://validator.w3.org/feed/


Copy and paste in the validator website our RSS Feed (which uses the below code to test it out:


Now that you understand that our feed is clean and validated, let's start the tutorial.


1 - Create a PHP file in your website root and call it 

rss-feed.php


2 -  In this file paste this code:

<?php require("database-connection.php"); ?>

<?php
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>';
?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="https://kollox.com/rss-feed.php" rel="self" type="application/rss+xml" />
<title>Your Website Title</title>
<link>https://Kollox.com</link>
<description>Add here a nice descriptino about your website / rss feed</description>
<language>en-us</language>
<copyright>Copyright (C) <?php echo date("Y") ?> Yourwebsite.com</copyright>


<?php
$activated = "Yes";  //Only show articles which are activated in your article database

$stmt = $conn->prepare("SELECT
article_title,
article_description,
article_url,
article_released_date
FROM articles
WHERE activated = ?
ORDER BY id DESC LIMIT 30");

$stmt->bind_param('s', $activated);

$stmt->bind_result(
$article_title,
$article_description,
$article_url,
$article_released_date);


$stmt->execute();
$stmt->store_result();


if($stmt->num_rows() > 0){
while ($stmt->fetch()) {
//My article dates are currently output from mysql as YYYY/MM/DD
//Let's change that to an atom format
$pubDate=  date("D, d M Y H:i:s O", strtotime($article_released_date));
echo "<item>"; echo "<title>".$article_title."</title>\n";
echo "<link>https://Kollox.com/".$article_url."</link>\n";
echo "<description>".$article_description."</description>\n";
echo "<guid isPermaLink='true'>https://Kollox.com/".$article_url."</guid>\n";
echo "<pubDate>".$pubDate."</pubDate>\n"; echo "</item>\n"; echo "\n\n"; }} $stmt->free_result(); $stmt->close(); ?> </channel> </rss>


Let me explain this code 

<?php require("database-connection.php"); ?>
This is the file that has all your MYSQL parameters, make sure this is set properly


<?php
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
Echo it rather than paste it as you might get conflict with your PHP shortcodes


$stmt = $conn->prepare("SELECT
article_title,
article_description,
article_url,
article_released_date
FROM articles
WHERE activated = ?
ORDER BY id DESC LIMIT 30");

$stmt->bind_param('s', $activated);

$stmt->bind_result(
$article_title,
$article_description,
$article_url,
$article_released_date);


$stmt->execute();
$stmt->store_result();
My database will be very different from yours, the fields above are actually not from our real database for security purpose, we gave simple names to each field for you to understand the concept easily. By adding a LIMIT of 30, we are limiting your rss feed to the latest 30 items which is more than enough.

In this example, from MYSQL, we will be pulling:
article_title
article_description
article_url
article_released_date

from a fictional table called "articles"

Later on we will be binding the results to the below variables so we can output these variables in our rss feed:
$article_title
$article_description,
$article_url,
$article_released_date


$pubDate=  date("D, d M Y H:i:s O", strtotime($article_released_date));
Our current database date is written as YYYY/MM/DD, we will change that to the ATOM format which is d M Y H:i:s




if($stmt->num_rows() > 0){
while ($stmt->fetch()) {
//My article dates are currently output from mysql as YYYY/MM/DD
//Let's change that to an atom format
$pubDate=  date("D, d M Y H:i:s O", strtotime($article_released_date));
echo "<item>";
echo "<title>".$article_title."</title>\n";
echo "<link>https://Kollox.com/".$article_url."</link>\n";
echo "<description>".$article_description."</description>\n";
echo "<guid isPermaLink='true'>https://Kollox.com/".$article_url."</guid>\n";
echo "<pubDate>".$pubDate."</pubDate>\n";
echo "</item>\n";
echo "\n\n";
}}
$stmt->free_result();
$stmt->close();

Finally, we will loop through the latest 30 articles and output them in the required format in our RSS feed.


Access your feed url and check it out to see how it works, if you struggle with the above contact us via facebook (in the comment box below).

Make sure you validate your feed using the link posted at the top of this article.