Solved

Retrieve data from MySQLl database in xml file

Posted on 2011-02-27
14
172 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
  • 6
  • 6
14 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:sam20
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Code is actuelly url and title is insted of description here. Title is in persian languge.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
I have added the column description (collate utf8_persian_ci) to the table.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

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

Author Comment

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

Expert Comment

by:CWS (haripriya)
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now