Mar 6, 2013

Building rss feed script using php and mysql

While browsing a forum i saw a question on how to build an rss feed for self made cms on php mysql.
First of all here it is the source that is going to be shown as plain text
<?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>
If you don't know how to use phpmyadmin please read how to post to database
So to explain
This is what we are going to build
// site title
// link
// description
   *** post 1 ***
   // title
   // link
   // description

   *** post 2 ***
   // title
   // link
   // description
// end
Go to phpmyadmin, create a new table, name it 'posts' and create the fields:
id (int, auto increment)
title (text)
link (text)
description (text)
Save it and then go to insert(phhmyadmin), and write 2 new posts, fill in all the fields except id.
Now create a php file called config.php, this file makes connection to 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
?>
Now 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>';
?>
You are done, for better preview view the php file in mozilla firefox, you can also use mod rewrite to change extension form .php to .xml or .rss

Edit! I forgot adding the header to rss.php so just paste this code in line 2
header("Content-Type: application/xml; charset=ISO-8859-1");

6 comments:

  1. Replies
    1. Please check out here. This code is working
      http://www.discussdesk.com/create-rss-feed-with-php-mysql.htm

      Delete
  2. XML Parsing Error: XML or text declaration not at start of entity

    ReplyDelete
  3. Good tuto, this is another one with source code:
    http://www.bewebdeveloper.com/tutorial-about-how-to-create-an-rss-feed-with-php-and-mysql

    ReplyDelete
  4. What should be the mime type for the result?

    ReplyDelete