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

by

Posted on 24 July 2016

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)
2-Title
3-description.
4-Link (For SEO friendly URL, learn more about SEO friendly URL at link)
5-Content.
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>http://www.example.com</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.

config.php

<?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');

RSS.php

<?php
header('Content-Type: application/xml');
?>
<rss version="2.0">
    <channel>
        <title>Your blog title</title>
        <link>http://www.psychocodes.in</link>
        <description>Psycho-CODES is your daily guide to computer programming and computer science.</description>
<?php
include "config.php";
$sql = "SELECT * FROM article.data 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 feedburner.google.com 
 
Now enter your feed URL in the input box, for example, your feeds URL will be 'http://www.example.com/RSS.php' 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.


If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts

Search
Adverstisement
Adverstisement