You are here:  » Store and Retrieve in Database


Store and Retrieve in Database

Submitted by shawnwalters on Tue, 2007-04-03 19:57 in

Hi,

You've helped me so much so far, I feel like a pest. Anyway, I'm trying to query a licensed xml news feed that we have and then store it in a sql database. Then retrieve that sql using php and display it on our website. I'm completely lost as to where to start. The code returned from the xml feed is:

  <item>
                <title>French Train Shatters Rail Record</title>
                <description>
                    <![CDATA[
                        <div>William Macklin - All Headline News Staff Reporter</div> <p>Paris, France (AHN) - Packing a 25,000-horsepower engine and running on over-sized wheels, a French train shattered the world speed record for conventional rail travel Tuesday as it hit a blistering 354.1 mph. </p><p>The double-decker, black and chrome high-speed train, dubbed the V150, easily shot beyond the previous record of 320.2 mph, as a small army of engineers and technicians milled around inside measuring the train's performance. </p><p>The train is the latest development in France's increasing use of bullet trains. The new TGV ("train a grande vitesse) has the eel-like body of its counterparts, but is outfitted with larger wheels intended to cover more track with each rotation, a hyped-up 25,000-horsepower engine, and gets its power from overhead cables with electrical tension goosed up from 25,000 volts to 31,000. </p><p>The new train's speed delighted the V150s owners, Alstom Transports, and pleased its operators. </p><p>"We saw the countryside go by a little faster than we did during the tests," the train's operator, Eric Pieczac, told the Associated Press. "I'm proud to have fulfilled the mission."</p><p>Before Tuesday's run, Alstom Transports had hoped to do more than just level the speed record for conventional train travel. The company had intimated that it wanted to level the playing field with a chief rival in the high-speed rail industry: Japan. </p><p>Since 2003, Japan has held the absolute speed record for a train. Unlike conventional trains which run on tracks, Japan's Maglev moves along a guideway soaring just above a powerful magnetic field. In 2003, a Maglev was clocked at 361 mph.</p>
                    ]]>
                </description>
                <link>snipped</link>
                <enclosure url="http://snipped.com/83261.jpg" length= "11454" type="image/jpeg"/>
                <source url="snipped">snipped</source>
                <pubDate>Tue, 03 Apr 2007 10:41:39 +0000</pubDate>
                <guid>http://snipped/7006936696</guid>
            </item>

I figured something like this would work based upon searching this site:

<?php
 
require("/home/uncover/public_html/MagicParser.php");
  
mysql_connect ("localhost""name""pass");
  
mysql_select_db ("db_name");
  function 
News($record)
  {
    
$sql "
      INSERT INTO full_pages SET
        title = '"
.mysql_real_escape_string$record["TITLE"] )."',
        description = '"
.mysql_real_escape_string$record["DESCRIPTION"] )."',
        url = '"
.mysql_real_escape_string$record["ENCLOSURE-URL"] )."',
        keywords = '"
.mysql_real_escape_stringsubstr($record["GUID"],40,10) )."',
      "
;
    
mysql_query($sql);
 if (@
mysql_query($sql)) {
     echo 
'<p>Your data has been added.</p>';
   } else {
     echo 
'<p>Error adding submitted data: ' mysql_error() . '</p>';
   }
  }  
  
MagicParser_parse("$url","News","xml|RSS/CHANNEL/ITEM/");
?>

But it doesnt work (obviously I used the correct sql data info, just omitted it here).

Any idea how to do this?

Thanks again,
Shawn

Submitted by support on Wed, 2007-04-04 07:06

Hi Shawn,

The structure of your code looks fine. The first question of course is does your print out of mysql_error() reveal anything?

Next, have you confirmed that there is no problem with the parsing - you can do this by putting a debug printout in the News() function to show that you are reading records.

One method I often use to debug is to print out the SQL that was constructed, and then paste this into the SQL box in phpMyAdmin - although that really only displays the same error message as you would get from mysql_error().

Hope this helps...
Cheers,
David.