Thursday, December 4, 2014

Developing RSS Feed Script Using PHP and MySQL

Want to know that how to build rss feed script using PHP and MySQL? First of all, check the below code.

RSS Feed Script Using PHP and MySQL

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
  <title>Rss feeds on php and mysql</title>
  <link>http://www.thecodertips.com</link>
  <description>Web developement tutorials</description>
  <item>
    <title>Using blogger tags to beautify template and creating plugins</title>
    <link>http://www.thecodertips.com/2013/03/using-blogger-tags-to-beautify-template.html</link>
    <description>Blogger tips</description>
  </item>
  <item>
    <title>Php tutorial, Login script with Jquery</title>
    <link>http://www.thecodertips.com/2012/11/php-tutorial-login-script-with-jquery.html</link>
    <description>Php script</description>
  </item>
</channel>
</rss>

First, you need to learn how to use phpmyadmin. Click Here to get a detail about it.  
We are going to build as per below :-

// site title
// link
// description
   *** post 1 ***
   // title
   // link
   // description

   *** post 2 ***
   // title
   // link
   // description
// end

Now go to phpmyadmin, create a new table, name it 'posts' and create the fields :-

id (int, auto increment)
title (text)
link (text)
description (text)

Then save it and go to insert and write two new posts except id fill in all the fields. 
Make config.php file, which makes a connection to the database.

<?php
mysql_connect("localhost", "root", "") or die("could not connect"); // host, user, password
mysql_select_db("database") or die("could not select database"); // database name
?>

At last need to create another php file, name it for ex rss.php

<?php
header("Content-Type: application/xml; charset=ISO-8859-1");
include("config.php");
?>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
  <title>Rss feeds on php and mysql</title>
  <link>http://www.thecodertips.com</link>
  <description>Web developement tutorials</description>
<?php
$query = mysql_query("SELECT title, link, description FROM posts");
while($row = mysql_fetch_array($query)){
echo '<item>
    <title>'.$row['title'].'</title>
    <link>'.$row['link'].'</link>
    <description>'.$row['description'].'</description>
  </item>';
}
echo '
</channel></rss>';
?>
Almost done. Check the preview in mozilla firefox. Furthermore, you can use mod rewrite to change extension form .php to .xml or .rss.

0 comments:

Post a Comment