Php RSS feed script for your Website

Generating an RSS feed for your website is very important for your SEO campaign.

When submitting your RSS feed to multiple Feed aggregates, your links get indexed much faster.

Today I 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/

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

rss-feed.php

2 – In this file paste this code:


';
?>




Your Website Title
https://Kollox.com
Add here a nice descriptino about your website / rss feed
en-us
Copyright (C)  Yourwebsite.com


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 "";
echo "".$article_title."\n";
echo "https://Kollox.com/".$article_url."\n";
echo "".$article_description."\n";
echo "https://Kollox.com/".$article_url."\n";
echo "".$pubDate."\n";
echo "\n";
echo "\n\n";
}}

$stmt->free_result();
$stmt->close();

?>


Let me explain this code

This is the file that has all your MYSQL parameters, make sure this is set properly
'; 
?>
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 my real database for security purpose, I 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 "";
echo "".$article_title."\n";
echo "https://Kollox.com/".$article_url."\n";
echo "".$article_description."\n";
echo "https://Kollox.com/".$article_url."\n";
echo "".$pubDate."\n";
echo "\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 from the comment box below.

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Disclaimer:

As an Amazon Associate I earn from qualifying purchases. This post may contain affiliate links which means I may receive a commission for purchases made through links.