Home arrow JavaScript arrow Page 3 - Revisited: Creating an XML Content Feed With PHP

Revisited: Creating an XML Content Feed With PHP

This article was originally published early this year and is one of our most popular articles, republished by popular demand. Have a read and you will see why...

Author Info:
By: Mitchell Harper
Rating: 3 stars3 stars3 stars3 stars3 stars / 9
October 28, 2002
  1. · Revisited: Creating an XML Content Feed With PHP
  2. · Why a content feed?
  3. · Creating a sample content feed
  4. · Creating a sample content feed (contd.)
  5. · Testing Our Content Feed
  6. · Conclusion

print this article

Revisited: Creating an XML Content Feed With PHP - Creating a sample content feed
(Page 3 of 6 )

One of the reasons why content feeds are so popular is that because 99% of the time, all you need to do to add someone elses content feed to your site is add a reference to an external JavaScript file using a HTML <script> tag, like this:

<script language="JavaScript" src="http://www.joe-bloggs-news.com/feed.php">

Let's assume that I run a small web site and want to publish Joe Blogg’s content feed on my site using the code above. In it, I've set the src attribute to a file named feed.php on Joe's web site (and not a typical JavaScript file that ends with .js).

This isn't a problem, however, because both IE and Netscape don't care what the file extension is, just as long as when the file is grabbed, it actually contains valid JavaScript code. Let's create a sample content feed now. To start with, load the MySQL console application and create a new database named content:

create database content;

use content;

Next, create a new table named articles. This table will contain all of our news items:

create table articles


articleId int auto_increment not null,

title varchar(100),

content text,

primary key(articleId),

unique id(articleId)


The table we've just created should be pretty self-explanatory. It contains three fields: articleId, an integer which is also a primary key and unique identifier. Title, which will contain the headline of each news item, and content, which is a text field that will contain the body of our news item.

Let's add three news items to our articles table using the following MySQL commands:

insert into articles values(0, 'PHP scripting language takes world by storm', 'It was announced today that at least two million web sites around the world are using the PHP and MySQL language/databasing technique to create sites that are flexible');

insert into articles values(0, 'Linux includes G++ compiler', 'Linux, one of the worlds most popular operating systems also includes a copy of both the GCC and G++ compilers, which allow you to compile C/C++ program easily from the command line');

insert into articles values(0, 'Microsoft announces C#', 'Microsoft have today announced that its new .NET framework will be based around a language named C# (pronounced C-Sharp). This language is much like C++ but designed for moder n prorgammers');

Next, we want to create the PHP script that will retrieve the news from our MySQL database and output it to the browser so that it appears as a bunch of JavaScript code. The PHP scripts name is feed.php and it is available for download here. We will concentrate only on the more important aspects of the script.

$db_server = "localhost";

$db_db = "content";

$db_user = "admin";

$db_pass = "password";

Our script starts of by creating four variables which will be used to login to our MySQL server. I'm assuming that MySQL is installed on the same machine as where the feed.php script will be setup.

@$sConn = mysql_connect($db_server, $db_user, $db_pass);

@$dConn = mysql_select_db($db_db, $sConn);

if(!$sConn || !$dConn)



document.write("Couldn't load news");



blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials