Solved

Retrieve data from MySQLl database in xml file

Posted on 2011-02-27
14
205 Views
Last Modified: 2013-11-30
I have a rss xml file which works fine please see below code. I will replace title, link, description and date with data from MySQL database.

<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://-.info/rss/Atom">
   <channel>
      <title>TITLE</title>
      <link>http://-.info/</link>
      <description>Whatever Company  News</description>
      <language>en-us</language>
      <pubDate>Mon, 4 Feb 2008 04:00:00 GMT</pubDate>

      <lastBuildDate>Mon, 4 Feb 2008 04:00:00 GMT</lastBuildDate>
      <docs>http://127.0.0.1/rss</docs>
      <generator>Weblog Editor 2.0</generator>
       
        <item>
         <title>News Article #1</title>
         <link>http://www.--.htm</link>
         <description>Whatever new article</description>
         <pubDate>Mon, 4 Feb 2008 04:00:00 GMT</pubDate>
         <guid>http://www.--.htm</guid>
      </item>
       
        <item>
         <title>News Article #2</title>
         <link>http://www.--.htm</link>
         <description>Whatever new article</description>
         <pubDate>Mon, 4 Feb 2008 04:00:00 GMT</pubDate>
         <guid>http://www.--.htm</guid>
      </item>
         
      </channel>
</rss>
0
Comment
Question by:sam20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
14 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34991610
This looks like RSS, not ATOM, so you might want to check the doctype declaration.

Here is how I would do it... Use HEREDOC syntax to create three strings.  The first string would contain the top of the XML and the channel info.  The second string would contain the XML that defines the item(s).  The third string would contain the closing tags for the channel and the rss.  Concatenate the strings as you retrieve information from the data base.
http://www.php.net/manual/en/language.types.string.php

Something like the (obviously untested and incomplete) code snippet might be used to create one of the item strings.
$sql = "SELECT my_title, my_link, my_description, my_date FROM my_table";
$res = mysql_query($sql) or die( mysql_error() );
while ($row = mysql_fetch_assoc($res))
{
    $t = htmlentities($row["my_title"]);
    $l = $row["my_link"];
    $d = htmlentities($row["my_description"]);
    $m = date('r', strtotime($row["my_date"]));

    $item = <<<ENDITEM
      <item>
         <title>$t</title>
         <link>$l</link>
         <description>$d</description>
         <pubDate>$m</pubDate>
         <guid>$l</guid>
      </item>
ENDITEM;

    echo $item;
}

Open in new window

0
 

Author Comment

by:sam20
ID: 34992036
Hi Ray, thanks for your reply. I'm not good at xml, I try display MySQL data in other sites and found the rss xml file. can you please tell me more abou the code. Should I run the code as a php file?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34992066
No, you should not run the code (obviously untested and incomplete).  You would need to adapt it to your specific application needs.  You can learn more about RSS here:
http://cyber.law.harvard.edu/rss/rss.html

If you already have the data base set up, and you can post the CREATE TABLE statements for the relevant table(s) we may be able to show you a better code example.  But it may take a long time since we do not have your test data!
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:sam20
ID: 34992128
CREATE TABLE `lastnews` (
  `id` int(12) unsigned NOT NULL auto_increment,
  `title` varchar(1500) character set utf8 collate utf8_persian_ci NOT NULL,
  `code` varchar(100) NOT NULL,
  `date` date NOT NULL,
  `time` time NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=496 ;
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 34992300
Thanks.  Looks like we are missing some information that would be needed for RSS.  Description and Link would be very useful.  You can probably omit them or provide an empty placeholder.

Important Question: Is the title really Persian and UTF-8?  If so you may want to allow some extra debugging time for character set issues.
0
 

Author Comment

by:sam20
ID: 34992370
Code is actuelly url and title is insted of description here. Title is in persian languge.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34992414
OK, if the title column is actually the description, you would probably want to add a column to this table to hold the title.  Read over the Harvard link carefully -- it's not perfect, but it is one of the best descriptions of RSS that you can get.
0
 

Author Comment

by:sam20
ID: 34992473
I have added the column description (collate utf8_persian_ci) to the table.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34993111
I think you might want two tables and a junction table.  One would be a channels table.  Each channel table would have a one-to-many relationship with the items table.  The column definitions would be similar because both channels and items have many characteristics in common, but there is a hierarchy - items belong to channels.

The junction table would contain two columns - the key of the channel row and the key of the item row.  Using this table you would be able to associate your channels and items in a "relational" way.

If you're new to web site development using PHP and MySQL, you would probably enjoy this book:
http://www.sitepoint.com/books/phpmysql4/
0
 

Author Comment

by:sam20
ID: 34995153
Hi, I retrieve data from mysql as shows in this way here http://khatesabz.info/, I Will rss to be able to show titles and their links in other websites.  
I will make those tables you have mentioned above.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34998174
Sounds good.  Best of luck with your project, ~Ray
0
 

Author Comment

by:sam20
ID: 34999022
The question is what should I do after I make those new tables?
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 39686716
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question