PHP/MySQL RSS Feed
Php Mysql Feedrss
	If you have a web site that is database driven with regularly updated material then it may be a good idea to setup an RSS feed for your website. This tutorial assumes you have a basic understanding of PHP and MySQL — otherwise how else did you get a database driven web site?
The first step is to use PHP to declare an XML document.
<?phpheader(“Content-type: text/xml”);echo“<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;	The ‘header’ sets the HTTP header to text/xml so the client (RSS reader) knows that the content-type is XML. The rest of the PHP sets the document type as XML version 1.0. The next step is to set the RSS version.
// Set RSS version.echo“<rss version=\”2.0\”>”;	Just in case you’re wondering why there is a line break after the ‘echo “’, it is to make the XML document code neater when it’s generated. The same system is used throughout this tutorial.
Next, we can begin to create the actual XML ocument.
// Start the XML.echo“   <channel>        <title>Feed title</title>        <description>A description of the feed contents</description>        <link>http://www.yoursite.com/</link>”;	What the above code does is create the opening ‘<channel>’ tag as well as the required ‘<title>’, ‘<description>’ and ‘<link>’ elements.
	Once the start of the document has been setup, it’s time to get the data that will fill the XML document from the database. I have a PHP file created that I use for all my database connections that I link to using the following.// Create a connection to your database.require(“db_connection.php”);
	The code that you would find on that page would be like this:<?phpmysql_connect(‘server’, ‘username’, ‘password’) or die(‘ERROR--CAN’T CONNECT TO SERVER’);mysql_select_db(‘database_name’) or die(‘ERROR--CAN’T CONNECT TO DB’);?>	The first line (‘mysql_connect...’) connects to the server while the second line (‘mysql_select_db...’) connects to your database. You need to replace the relevant information with your own server/database details for this to work.
Now we can begin to add the data that gets pulled from the database.
// Query database and select the last 10 entries.$data = mysql_query(“SELECT * FROM table ORDER BY id DESC LIMIT 10”);while($row = mysql_fetch_array($data)){	/* Convert database images data into actual image link. */	$row[Intro] = str_replace(“images/”, “http://www.yoursite.com/images/”,$row[Intro]);
	What we have done now is query the database and selected everything from the table named ‘table’ but limited the results to the last 10 (ordered latest to oldest by their id). You would need to edit ‘table’ to the name of the table you want to create a feed fom.	‘$row[Intro]’ assumes you have a column in your table called ‘Intro’. You would need to rename this to whatever the column name is that contains the data you want to have displayed in the RSS reader.	The str_replace function replaces all instances of ‘images/’ with ‘http://www.yoursite.com/images/’ which is very important if the column you are getting data from in your database contains links to images on your site. If this is not the case, feel free to ignore that line of code. And if you have a different location for your images, edit accordingly.
The next part is where the XML document is filled with data.
/* Continue with the 10 items to be included in the <item> section of the XML. */echo“   <item>	<link>http://www.yoursite.com/article.php?id=”.$row[id].”</link>	<guid isPermaLink=\”true\”>http://www.yoursite.com/article.php?id=”.$row[id].”</guid>	<title>”.$row[Title].”</title><description><![CDATA[“.$row[Intro].”]]></description>	<comments>http://www.yoursite.com/article.php?id=”.$row[id].”#Comments</comments>	</item>”;}
	The code above creates the ‘<item>’ and its required ‘<link>’, ‘<title>’ and ‘<description>’ elements. I have also included the ‘<guid>’ element as it is recommended by the Feed Validator. The ‘<guid>’ element uniquely identifies the item. In my case, the link URL is always unique and therefore works well as the ‘<guid>’. If the link to the article will always remain the same, then set ‘isPermaLink’ to ‘true’. If the URL will change, set it to ‘false’. You will need to edit the ‘<link>’ and ‘<guid>’ elements to display whatever web pages your site uses.	The contents of the ‘<link>’ element contain the URL of the full document you are linking to. ‘<title>’ displays the title of the document (if your title field is not named ‘Title’ then rename ‘$row[Title]’ to suit your database).
	The ‘<description>’ element contains the text from the article that you want to have displayed in the RSS reader. An important part of this element’s contents is the CDATAsection. Normally, XML parses all of the text contained within it. So if your text contains characters like ‘<’ or ‘&’ then your description will be broken. If you plan on including images in your feed, then you’ll need to include the CDATA code.
	If the text you are including in your description can be very lengthy you may also want to consider using ‘substr’. So instead of displaying ‘<description><![CDATA[“.$row[Intro].”]]></description>’ you would show ‘<description> <![CDATA[“.substr($row[Intro],0,150).”]]></description>’. What the substr code does is start from the first character (represented by the ‘0’) and counts to the 150th character (represented by the ‘150’). Anything after the 150th character is not displayed. Again, feel free to change the ‘150’ to any number that suits your needs.
	Finally, another optional element I have included is the ‘<comments>’ element. IftheURL of your articl e allows for comments you can include a link to where the comments can be found.	To get a full description of all the different tags available in RSS, check http://feedvalidator.org/docs/.	All that is left to do now is close the ‘<channel>’ element and RSS document using PHP.
echo“</channel></rss>”;?>	You have now created an XML document/RSS feed. I have included the completed code on the following page. Please note, this tutorial does contain formatted text which you will need to replace if you plan on copying and pasting it into a program like Dreamweaver. Code such as “ and ” will have to be replaced (use Ctrl+Fin Dreamweaver to replace the text) so that it becomes unformatted.
<?phpheader(“Content-type: text/xml”);echo“<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;// Set RSS version.echo“<rss version=\”2.0\”> “;// Start the XML.echo“<channel><title>Feed title</title><description>A description of the feed contents</description><link>http://www.yoursite.com/</link>”;// Create a connection to your database.
require(“db_connection.php“);// Query database and select the last 10 entries.$data = mysql_query(“SELECT * FROM table ORDER BY id DESC LIMIT 10”);while($row = mysql_fetch_array($data)){// Convert database images data into actual image link.$row[Intro] = str_replace(“images/”, “http://www.yoursite.com.au/images/”, $row[Intro]);// Continue with the 10 items to be included in the <item> section of the XML.
echo “<item><link>http://www.yoursite.com/article.php?id=”.$row[id].”</link><guid isPermaLink=\”true\”>http://www.yoursite.com/article.php?id=”.$row[id].”</guid><title>”.$row[Title].”</title><description><![CDATA[“.$row[Intro].”]]></description><comments>http://www.yoursite.com/article.php?id=”.$row[id].”#Comments</comments></item>”;}
// substr can limit the number of characters. First number is starting point while the second number is number ofcharacters.// otherwise just use $row[Intro].// <guid> is an optional sub-element of <item> but recommended if you don’t want to receive a warning whenvalidating your RSS.echo“</channel></rss>”;?>
Php Mysql Feedrss

More Related Content

PDF
Ibm tivoli access manager for e business junctions and links redp4621
PPT
SQL Injection Attacks
PPT
URL
PPT
PPTX
Data Applied: Developer Quicklook
PPT
OpenSearch
PPTX
Links in Html
DOC
Asp.Net The Data List Control
Ibm tivoli access manager for e business junctions and links redp4621
SQL Injection Attacks
URL
Data Applied: Developer Quicklook
OpenSearch
Links in Html
Asp.Net The Data List Control

What's hot (12)

PPT
Internet with HTML
PPTX
Html (hypertext markup language)
PPTX
Hyperlink
PDF
Html links
PPTX
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
PPTX
HTML5 - create hyperlinks and anchors
PDF
Forms and Databases in PHP
PDF
Just Enough HTML for Fatwire
PPT
ASP.NET MVC introduction
PPTX
Xml For Dummies Chapter 4 Adding Xhtml For The Web
PPTX
LINKING IN HTML
PPTX
JSON and REST
Internet with HTML
Html (hypertext markup language)
Hyperlink
Html links
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
HTML5 - create hyperlinks and anchors
Forms and Databases in PHP
Just Enough HTML for Fatwire
ASP.NET MVC introduction
Xml For Dummies Chapter 4 Adding Xhtml For The Web
LINKING IN HTML
JSON and REST
Ad

Similar to Php Mysql Feedrss (20)

PPT
Creating an RSS feed
PPT
Php Rss
PDF
jsp tutorial
PPT
PPT
Transforming Xml Data Into Html
PPTX
Ellerslie User Group - ReST Presentation
PDF
php-mysql-tutorial-part-3
PDF
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
PDF
php-mysql-tutorial-part-3
PDF
<img src="../i/r_14.png" />
PPT
Solr Presentation
PPT
PPT
XML Transformations With PHP
PPT
2310 b 12
PPT
JSP diana y yo
PDF
Sitepoint.com a basic-html5_template
PPT
Xhtml Part2
PDF
web design for html to second yea for college
PPTX
1 Introduction to Drupal Web Development
Creating an RSS feed
Php Rss
jsp tutorial
Transforming Xml Data Into Html
Ellerslie User Group - ReST Presentation
php-mysql-tutorial-part-3
<b>PHP</b>/MySQL <b>Tutorial</b> webmonkey/programming/
php-mysql-tutorial-part-3
<img src="../i/r_14.png" />
Solr Presentation
XML Transformations With PHP
2310 b 12
JSP diana y yo
Sitepoint.com a basic-html5_template
Xhtml Part2
web design for html to second yea for college
1 Introduction to Drupal Web Development
Ad

Recently uploaded (20)

PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPT
What is a Computer? Input Devices /output devices
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
Configure Apache Mutual Authentication
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPT
Geologic Time for studying geology for geologist
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Developing a website for English-speaking practice to English as a foreign la...
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Taming the Chaos: How to Turn Unstructured Data into Decisions
A review of recent deep learning applications in wood surface defect identifi...
What is a Computer? Input Devices /output devices
Microsoft Excel 365/2024 Beginner's training
Hindi spoken digit analysis for native and non-native speakers
Configure Apache Mutual Authentication
CloudStack 4.21: First Look Webinar slides
Getting started with AI Agents and Multi-Agent Systems
NewMind AI Weekly Chronicles – August ’25 Week III
sbt 2.0: go big (Scala Days 2025 edition)
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Consumable AI The What, Why & How for Small Teams.pdf
Enhancing emotion recognition model for a student engagement use case through...
Custom Battery Pack Design Considerations for Performance and Safety
A contest of sentiment analysis: k-nearest neighbor versus neural network
sustainability-14-14877-v2.pddhzftheheeeee
Geologic Time for studying geology for geologist
Chapter 5: Probability Theory and Statistics
Developing a website for English-speaking practice to English as a foreign la...

Php Mysql Feedrss

  • 3. If you have a web site that is database driven with regularly updated material then it may be a good idea to setup an RSS feed for your website. This tutorial assumes you have a basic understanding of PHP and MySQL — otherwise how else did you get a database driven web site?
  • 4. The first step is to use PHP to declare an XML document.
  • 5. <?phpheader(“Content-type: text/xml”);echo“<?xml version=\”1.0\” encoding=\”UTF-8\”?>”; The ‘header’ sets the HTTP header to text/xml so the client (RSS reader) knows that the content-type is XML. The rest of the PHP sets the document type as XML version 1.0. The next step is to set the RSS version.
  • 6. // Set RSS version.echo“<rss version=\”2.0\”>”; Just in case you’re wondering why there is a line break after the ‘echo “’, it is to make the XML document code neater when it’s generated. The same system is used throughout this tutorial.
  • 7. Next, we can begin to create the actual XML ocument.
  • 8. // Start the XML.echo“ <channel> <title>Feed title</title> <description>A description of the feed contents</description> <link>http://www.yoursite.com/</link>”; What the above code does is create the opening ‘<channel>’ tag as well as the required ‘<title>’, ‘<description>’ and ‘<link>’ elements.
  • 9. Once the start of the document has been setup, it’s time to get the data that will fill the XML document from the database. I have a PHP file created that I use for all my database connections that I link to using the following.// Create a connection to your database.require(“db_connection.php”);
  • 10. The code that you would find on that page would be like this:<?phpmysql_connect(‘server’, ‘username’, ‘password’) or die(‘ERROR--CAN’T CONNECT TO SERVER’);mysql_select_db(‘database_name’) or die(‘ERROR--CAN’T CONNECT TO DB’);?> The first line (‘mysql_connect...’) connects to the server while the second line (‘mysql_select_db...’) connects to your database. You need to replace the relevant information with your own server/database details for this to work.
  • 11. Now we can begin to add the data that gets pulled from the database.
  • 12. // Query database and select the last 10 entries.$data = mysql_query(“SELECT * FROM table ORDER BY id DESC LIMIT 10”);while($row = mysql_fetch_array($data)){ /* Convert database images data into actual image link. */ $row[Intro] = str_replace(“images/”, “http://www.yoursite.com/images/”,$row[Intro]);
  • 13. What we have done now is query the database and selected everything from the table named ‘table’ but limited the results to the last 10 (ordered latest to oldest by their id). You would need to edit ‘table’ to the name of the table you want to create a feed fom. ‘$row[Intro]’ assumes you have a column in your table called ‘Intro’. You would need to rename this to whatever the column name is that contains the data you want to have displayed in the RSS reader. The str_replace function replaces all instances of ‘images/’ with ‘http://www.yoursite.com/images/’ which is very important if the column you are getting data from in your database contains links to images on your site. If this is not the case, feel free to ignore that line of code. And if you have a different location for your images, edit accordingly.
  • 14. The next part is where the XML document is filled with data.
  • 15. /* Continue with the 10 items to be included in the <item> section of the XML. */echo“ <item> <link>http://www.yoursite.com/article.php?id=”.$row[id].”</link> <guid isPermaLink=\”true\”>http://www.yoursite.com/article.php?id=”.$row[id].”</guid> <title>”.$row[Title].”</title><description><![CDATA[“.$row[Intro].”]]></description> <comments>http://www.yoursite.com/article.php?id=”.$row[id].”#Comments</comments> </item>”;}
  • 16. The code above creates the ‘<item>’ and its required ‘<link>’, ‘<title>’ and ‘<description>’ elements. I have also included the ‘<guid>’ element as it is recommended by the Feed Validator. The ‘<guid>’ element uniquely identifies the item. In my case, the link URL is always unique and therefore works well as the ‘<guid>’. If the link to the article will always remain the same, then set ‘isPermaLink’ to ‘true’. If the URL will change, set it to ‘false’. You will need to edit the ‘<link>’ and ‘<guid>’ elements to display whatever web pages your site uses. The contents of the ‘<link>’ element contain the URL of the full document you are linking to. ‘<title>’ displays the title of the document (if your title field is not named ‘Title’ then rename ‘$row[Title]’ to suit your database).
  • 17. The ‘<description>’ element contains the text from the article that you want to have displayed in the RSS reader. An important part of this element’s contents is the CDATAsection. Normally, XML parses all of the text contained within it. So if your text contains characters like ‘<’ or ‘&’ then your description will be broken. If you plan on including images in your feed, then you’ll need to include the CDATA code.
  • 18. If the text you are including in your description can be very lengthy you may also want to consider using ‘substr’. So instead of displaying ‘<description><![CDATA[“.$row[Intro].”]]></description>’ you would show ‘<description> <![CDATA[“.substr($row[Intro],0,150).”]]></description>’. What the substr code does is start from the first character (represented by the ‘0’) and counts to the 150th character (represented by the ‘150’). Anything after the 150th character is not displayed. Again, feel free to change the ‘150’ to any number that suits your needs.
  • 19. Finally, another optional element I have included is the ‘<comments>’ element. IftheURL of your articl e allows for comments you can include a link to where the comments can be found. To get a full description of all the different tags available in RSS, check http://feedvalidator.org/docs/. All that is left to do now is close the ‘<channel>’ element and RSS document using PHP.
  • 20. echo“</channel></rss>”;?> You have now created an XML document/RSS feed. I have included the completed code on the following page. Please note, this tutorial does contain formatted text which you will need to replace if you plan on copying and pasting it into a program like Dreamweaver. Code such as “ and ” will have to be replaced (use Ctrl+Fin Dreamweaver to replace the text) so that it becomes unformatted.
  • 21. <?phpheader(“Content-type: text/xml”);echo“<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;// Set RSS version.echo“<rss version=\”2.0\”> “;// Start the XML.echo“<channel><title>Feed title</title><description>A description of the feed contents</description><link>http://www.yoursite.com/</link>”;// Create a connection to your database.
  • 22. require(“db_connection.php“);// Query database and select the last 10 entries.$data = mysql_query(“SELECT * FROM table ORDER BY id DESC LIMIT 10”);while($row = mysql_fetch_array($data)){// Convert database images data into actual image link.$row[Intro] = str_replace(“images/”, “http://www.yoursite.com.au/images/”, $row[Intro]);// Continue with the 10 items to be included in the <item> section of the XML.
  • 24. // substr can limit the number of characters. First number is starting point while the second number is number ofcharacters.// otherwise just use $row[Intro].// <guid> is an optional sub-element of <item> but recommended if you don’t want to receive a warning whenvalidating your RSS.echo“</channel></rss>”;?>