Publishing System Settings Logout Login Register
Creating A Php-sql Rss Feed
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on January 26th, 2007
7284 views
PHP Coding
In this tutorial you will learn how to create a table in phpMyAdmin and create an RSS feed that goes along with it!
Requirements: Know basics of PHP, phpMyAdmin, notepad, mouse and keyboard.

Now let�s start with our SQL Table.

CREATE TABLE `news` (
  `id` tinyint(11) NOT NULL auto_increment,
  `title` text NOT NULL,
  `author` text NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`id`)
);

That simply created a table called news. The table news contains 4 rows. Each row stands for something different. So in this case, we have 4 rows.


Click to enlarge

Now lets fill the table with some random text so we can see how this RSS works.

INSERT INTO `news` VALUES (1, �test1', 'BigDog', ' This is Test 1');
INSERT INTO `news` VALUES (2, �test2', 'BigDog', ' This is Test 2');
INSERT INTO `news` VALUES (3, �test3', 'BigDog', ' This is Test 3');

If you don�t understand what I did above, I just simply used INSERT TO and inserted values into each row.




Now that we have the table setup, we can start with our RSS Feed for it. Since this is going to be a .php file instead of .xml, we need to specify the server that this file will have xml content.

<? header('Content-type: text/xml'); ?>


Now lets connect to our database.

<?php
$dbhost = "localhost"; // almost always localhost.
$dbname = "news";      // Database Name, In our case, its news
$dbuser = "UserName";  // Database Username
$dbpass = "Password";  // Databse Password

$connect = mysql_connect("$dbhost","$dbuser","$dbpass");// Connecting to Database
mysql_select_db($dbname) or die (mysql_error());          //  Selecting Database
?>





Now that we have specified our username, password, and table, we can start the main rss.


<rss version="2.0">
    <channel>
         <title>Test Title</title>
         <description>This is an example description</description>
         <link>http://pixel2life.com</link>

This code above is like any other rss/xml feed. We are using rss version 2.0. You can change title, description, and link to your needs. Title can be anything but normally it�s your site name. Give a little information about your site in the description and your websites link in the <link>


<?
$sql = "SELECT * FROM news limit 5"; 
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
?>

The above code is simply getting a query from our sql table. You can change
$sql = "SELECT * FROM news limit 5" to anything you want! Change the limit, table, or even add more information like WHERE author = 'BigDog'. That will only show the submissions by BigDog.




Hold on, we are almost done!
Now lets get the main rss.


<item>
<title><?=$row['title']; ?></title>
<author><?=$row['author']; ?></author>
<link>http://MYSITE.com/news.php?id=<?=$row['id']; ?></link>
</item>

Now lets explain.
the <item> tag defines an row the RSS/SQL feed. Since we have it so it would show 3 rows from our table, at the end you will have 3 <item> tags.
 
The <title> tag is what will show up in the rss. If you use Mozilla Firefox, it�s the name that shows up when you select the drop down menu.
The <author> tag shows the owner of the news or content. In our case, it would be BigDog.
The <link> tag will show the ID for us. But as you can, I've added a link before it. That link is for my news. So If I have a page called news.php and news.php?id=# will show the specified news, that will link it to the specified contact/news.

And that�s it. Now let�s end our tags and we are done! Just make sure you don't miss a ending tag.

<?
}
?>

</channel>
</rss>





Here is our final code!

<? header('Content-type: text/xml'); ?>

<?php
$dbhost = "localhost"; // almost always localhost.
$dbname = "news"; // Database Name, In our case, its news
$dbuser = "UserName"; // Database Username
$dbpass = "Password"; // Databse Password

$connect = mysql_connect("$dbhost","$dbuser","$dbpass");// Connecting to Database
mysql_select_db($dbname) or die (mysql_error()); // Selecting Database
?>

<rss version="2.0">
<channel>
    <title>Test Title</title>
    <description>This is an example description</description>
    <link>http://pixel2life.com</link>

<?
$sql = "SELECT * FROM news limit 5";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
?>

<item>
     <title><?=$row['title']; ?></title>
     <author><?=$row['author']; ?></author>
     <link>http://MYSITE.com/news.php?id=<?=$row['id']; ?></link>
</item>

<?
}
?>

</channel>
</rss>




Thank you for reading. You may download the PHP and the SQL file by clicking the link below:
File Download: Rss.php and database.sql
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
BigDog

My name is Nick aka BigDog. I help run a gaming website called "FPSBANANA" or "FPSB". Its a FPS (first person shooter) website.

I love to code and try my best to learn new and useful languages. I am currently learning Java in school and hoping to get
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top