Help - Search - Members - Calendar
Full Version: Creating A Php-sql Rss Feed
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
BigDog
Please view the updated version here

__________________________________________________________________

This tutorial was on my site before It went down and I've decided to bring it here. Hope you like victory.gif
__________________________________________________________________

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.
CODE
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.
CODE
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.
CODE
<? header('Content-type: text/xml'); ?>


Now lets connect to our database.
CODE
<?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.

CODE
<rss version="2.0">
    <channel>
        <title>Dark Pixels Tutorials</title>
     <description>Latest tutorial on Dark Pixels</description>
      <link>http://darkpixels.net/</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>

CODE
<?
$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.

CODE
<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!
CODE
<?
}
?>

</channel>
</rss>


Here is our final code!
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.
-cherries-
why are you using tinyint?
it be just as easy using int.


CREATE TABLE news (
id int NOT NULL auto_increment,
title text NOT NULL,
author text NOT NULL,
content text NOT NULL,
PRIMARY KEY (id)
)

that would work just as well.
Matthew.
It must be tinyint as if you use INT then the max number of entries you can have is 127. You will just get a duplicate key error when you reach that amount.


tinyint is much much higher.
BigDog
:-) Ya, I did INT first then changed it to tinyint.
Indigo
QUOTE
Requirements: Know basics of php, phpMyAdmin, notepad, mouse and keyboard.


OH NO, NOT MOUSE AND KEYBOARD ohmy.gif
Ok, seriously: This seems rather good. Thanks for sharing!
BigDog
I know I know, I rased the requirements to a mouse and keyboard. I am so sorry tongue.gif

And you're welcome.
α∂αмяoss
w00t, finally a tutorial that is explained and not another long piece of code snippet!
ChrisGilmore
Thanks, I spent the whole day once trying to find how to create RSS feeeds which explained it. I love it.
BigDog
Thanks for the feedback guys.
α∂αмяoss
BTW. Is this code RSS validated?
BigDog
No its not RSS validated and I am not worried about validation. If you are, you can go ahead and change the code to fit validation. This is simply suppose to teach people how to make one and I don't think any beginner wants to be perfect.
α∂αмяoss
Lol ok...
Faken
Thanks for posting this BigDog smile.gif Hope you will move this to the publishing system when we move bigwink.gif

Dan
BigDog
I had not been at P2L for a bit and had to catch up on the reading.

Like the publishing system and I will definitely move this to the Publishing system section.

And you are welcome.
bfreddyberg
QUOTE(.Matt @ Aug 2 2006, 05:28 PM) *
It must be tinyint as if you use INT then the max number of entries you can have is 127. You will just get a duplicate key error when you reach that amount.

tinyint is much much higher.


Ummm you have that a little backwards. tinyint(length) can store integers from -128 to 127. But as int(length)
can store integers from -2147483648 to +2147483647, but can be limited commonly as 11.

So to correct you int is much much higher, as tinyint is what you would expect it to be....tiny.

Sorry I just couldn't let this slip my mind letting people have false information.


Great tutorial there BigDog! Works like a charm just retrieving data from the news table i already had! victory.gif
BigDog
Thank you bfreddyberg, means a lot. Glad it works with your system. I tried to make it as easiest and it can get :-)
Gravity
Thanks, This is very usefull.
Figgy
QUOTE(.Matt @ Aug 2 2006, 06:28 PM) *
It must be tinyint as if you use INT then the max number of entries you can have is 127. You will just get a duplicate key error when you reach that amount.


tinyint is much much higher.


Erm, please look up these kind of things before you say something!
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

tinyint -> 127 entries
int -> 2147483647 entries
bigint -> 9223372036854775807 entries

Int is more than enough - tinyint is not.
Have a nice day!

Edit: Ok, I just noticed there was a post that corrected your misstake, but niether could I let you give false information.
Mahmood
nice smile.gif
Thanks for the tutorial... I was looking for this...
Anobiz
I was just wondering abt rss feeds victory.gif thanks very much for your effort and yes I will remember I learnt it from BigDog whenever I'm going to use RSS feeds rolleyes.gif
DanCS3
I used the script here, but i get Feed Code Error O.o
BigDog
Please refer to the updated version here:
http://www.pixel2life.com/publish/tutorial...p_sql_rss_feed/
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.