Creating A Php-sql Rss Feed, Learn how to create a full PHP-SQL RSS feed. |
Aug 2 2006, 12:23 PM
Post
#1
|
|
|
Young Padawan ![]() Group: Members
Posts: 276
Joined: 17-August 05
From: Orange County, California
Member No.: 9,995
Publishing: My Tutorials
|
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 __________________________________________________________________ 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. This post has been edited by Faken: Feb 12 2007, 09:58 PM |
|
|
|
Aug 2 2006, 12:26 PM
Post
#2
|
|
|
Banned ![]() Group: Banned
Posts: 17
Joined: 12-December 05
Member No.: 14,181
Publishing: My Tutorials
|
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. |
|
|
|
Aug 2 2006, 12:28 PM
Post
#3
|
|
|
Official Spammer .Matt ![]() ![]() ![]() ![]() Group: Members
Posts: 2,727
Joined: 13-May 06
From: England
Member No.: 18,648
Publishing: My Tutorials
|
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. This post has been edited by .Matt: Aug 2 2006, 12:32 PM |
|
|
|
Aug 2 2006, 12:38 PM
Post
#4
|
|
|
Young Padawan ![]() Group: Members
Posts: 276
Joined: 17-August 05
From: Orange County, California
Member No.: 9,995
Publishing: My Tutorials
|
:-) Ya, I did INT first then changed it to tinyint.
|
|
|
|
Aug 3 2006, 04:54 PM
Post
#5
|
|
|
Official Alien ![]() ![]() ![]() Group: Members
Posts: 617
Joined: 17-July 05
From: Trondheim, Norway
Member No.: 8,681
Publishing: My Tutorials
|
QUOTE Requirements: Know basics of php, phpMyAdmin, notepad, mouse and keyboard. OH NO, NOT MOUSE AND KEYBOARD Ok, seriously: This seems rather good. Thanks for sharing! |
|
|
|
Aug 3 2006, 06:06 PM
Post
#6
|
|
|
Young Padawan ![]() Group: Members
Posts: 276
Joined: 17-August 05
From: Orange County, California
Member No.: 9,995
Publishing: My Tutorials
|
I know I know, I rased the requirements to a mouse and keyboard. I am so sorry
And you're welcome. |
|
|
|
Aug 13 2006, 03:53 PM
Post
#7
|
|
|
P2L Jedi Master ![]() ![]() ![]() ![]() Group: Members
Posts: 2,102
Joined: 30-July 05
From: $_SERVER['REMOTE_ADDR']
Member No.: 9,243
Publishing: My Tutorials
|
w00t, finally a tutorial that is explained and not another long piece of code snippet!
|
|
|
|
Aug 15 2006, 04:50 AM
Post
#8
|
|
|
Young Padawan ![]() Group: Publishing Betazoids
Posts: 147
Joined: 7-August 06
From: Nottinghamshire, East Midlands
Member No.: 21,452
Publishing: My Tutorials
|
Thanks, I spent the whole day once trying to find how to create RSS feeeds which explained it. I love it.
|
|
|
|
Aug 16 2006, 05:24 PM
Post
#9
|
|
|
Young Padawan ![]() Group: Members
Posts: 276
Joined: 17-August 05
From: Orange County, California
Member No.: 9,995
Publishing: My Tutorials
|
Thanks for the feedback guys.
|
|
|
|
Aug 17 2006, 04:42 PM
Post
#10
|
|
|
P2L Jedi Master ![]() ![]() ![]() ![]() Group: Members
Posts: 2,102
Joined: 30-July 05
From: $_SERVER['REMOTE_ADDR']
Member No.: 9,243
Publishing: My Tutorials
|
BTW. Is this code RSS validated?
|
|
|
|
Aug 17 2006, 07:17 PM
Post
#11
|
|
|
Young Padawan ![]() Group: Members
Posts: 276
Joined: 17-August 05
From: Orange County, California
Member No.: 9,995
Publishing: My Tutorials
|
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.
|
|
|
|
Aug 18 2006, 07:53 AM
Post
#12
|
|
|
P2L Jedi Master ![]() ![]() ![]() ![]() Group: Members
Posts: 2,102
Joined: 30-July 05
From: $_SERVER['REMOTE_ADDR']
Member No.: 9,243
Publishing: My Tutorials
|
Lol ok...
|
|
|
|
Nov 9 2006, 11:16 AM
Post
#13
|
|
|
Pimpmaster G Group: Admin
Posts: 5,381
Joined: 17-December 03
From: Montreal, Canada
Member No.: 2
Publishing: My Tutorials
|
Thanks for posting this BigDog
Dan |
|
|
|