You are here:  » importing feed into database with magicparser


importing feed into database with magicparser

Submitted by jonny5 on Sat, 2008-08-23 16:12 in

Hi David , my question is regarding magicparser , did try to post on the other forum but i was having problems logging in.
anyway i hop it is ok to post here , i have used pricetapestry lots and understand how that works but have never used magicparser or really know what it can do :)

i found the below code on magicparser forum and i believe this is what i need , and from looking at it i think i can happily edit the bits i need to make it work for me.
so the below code will enter a datafeed into a database? is that correct
does the feed need to be on my server first or can it be a url feed?
also will the below code delete the old records only from this feed when it is executed , if not can it?

sorry for all the questions but never used it before :)

cheers

<?php
  
require("MagicParser.php");
  
mysql_connect ("localhost""...my username here...""...my password here...");
  
mysql_select_db ("...the database name here...");
  function 
myRecordHandler($villa)
  {
    
$sql "
      INSERT INTO villas SET
        prop_ref = '"
.mysql_real_escape_string$villa["prop_ref"] )."',
        owner_ref = '"
.mysql_real_escape_string$villa["owner_ref"] )."',
        Title = '"
.mysql_real_escape_string$villa["Title"] )."',
        Summary = '"
.mysql_real_escape_string$villa["Summary"] )."',
        ImageLocation = '"
.mysql_real_escape_string$villa["ImageLocation"] )."',
        Filename = '"
.mysql_real_escape_string$villa["Filename"] )."'
      "
;
    
mysql_query($sql);
  }
  
mysql_query("DELETE FROM villas");
  
MagicParser_parse("files/xml/italian_villas.xml","myRecordHandler","xml|VILLARESULTS/VILLAS/VILLA/");
?>

Submitted by support on Sun, 2008-08-24 11:21

Hi Jonny,

Yes - that script is a complete feed > database framework using Magic Parser. Obviously it is very bespoke in terms of database tables and field names to the feed in the example (an accommodation feed) so that would all need to be changed to suit your data mode. The line:

  mysql_query("DELETE FROM villas");

...is the initial delete everything before importing. As Magic Parser just uses fopen() to access the file to parse; it can be a local file or URL if URL Wrappers are enabled on your PHP installation (they normally are!)

Cheers,
David.

Submitted by jonny5 on Sun, 2008-08-24 13:31

great will have a go , seems simple enough :)

Submitted by jonny5 on Sun, 2008-08-24 15:33

i have other things in the database as well so if i run this with one feed will it delete all the other data in the database??

Submitted by support on Tue, 2008-08-26 07:23

Hi Jonny,

In this case, what you need to do is delete selectively before you begin reading each feed. Presumably you would know which merchant (if that is what you call them in your application) is about to be imported; so rather than deleting everything using something like:

  mysql_query("DELETE FROM villas");

...you would just do:

  mysql_query("DELETE FROM villas WHERE merchant='Some Merchant'");

Cheers,
David.

Submitted by marco on Fri, 2016-10-07 13:52

Hello,

Its an old post, but i am trying to get this to work in php7.
I have changed it to:

<?php
  
require("MagicParser.php");
  
$conn mysql_connect ("localhost""...my username here...""...my password here…”, "...the database name here...");
  function myRecordHandler(
$villa)
  {
    
$sql = "
      
INSERT INTO villas SET
        prop_ref 
'".mysql_real_escape_string( $villa["prop_ref"] )."',
        
owner_ref '".mysql_real_escape_string( $villa["owner_ref"] )."',
        
Title '".mysql_real_escape_string( $villa["Title"] )."',
        
Summary '".mysql_real_escape_string( $villa["Summary"] )."',
        
ImageLocation '".mysql_real_escape_string( $villa["ImageLocation"] )."',
        
Filename '".mysql_real_escape_string( $villa["Filename"] )."'
      ";
    mysqli_query(
$conn$sql);
  }
  mysqli_query(
$conn, “DELETE FROM villas");
  
MagicParser_parse("files/xml/italian_villas.xml","myRecordHandler","xml|VILLARESULTS/VILLAS/VILLA/");
?>

But does not work. What am i missing?

(13/03A with Forward Compatibility Patch)

Best,
Marco

Submitted by support on Sat, 2016-10-08 08:47

Hi Marco,

$conn isn't declared as global at the top of the myRecordHandler function - add the following line immediate inside the function:

  global $conn;

That should be all it is...

Cheers,
David.
--
PriceTapestry.com

Submitted by marco on Sat, 2016-10-08 10:30

Thanks!
Best,
Marco