Looking for PHP code to import xml to mysql

Discussion in 'PHP' started by thesportsman, Apr 29, 2011.

  1. #1
    Hi all. This is my first post so thanks for the help!

    I have a project I would like to do, but need some code to help out. I have an xml file that contains a lot of information. 16000 products. What I would like to do it to pull the information from xml file then insert it into the mysql db on my website.

    A few things need to be changed though.

    I need to combine some of the fields in the xml.

    For example product name in the db would be "microsoft windows" from the xml <mfg>microsoft</mfg>+<name>windows</name> is what needs to be combined.

    How would I combine those before inserting them into the db.

    Or price how to add something to a number in the xml before it is inserted in the db.

    example <price>10.00</price>*25% so that 12.50 is inserted into the db.

    Thanks
     
    thesportsman, Apr 29, 2011 IP
  2. thesportsman

    thesportsman Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Would something like this work? If so how would I edit the price for example?

    $id = $prod->prod['id'];
    $description = $prod->name;
    $image = $prod->awImage;
    $fulldescription = $prod->desc;
    
    //insert query
    if(strlen($prod) > 0)
    
    {
    $query = mysql_query("UPDATE productfeed SET description = '$description', fulldescription = '$fulldescription', image = '$image' WHERE id = '$id'") or die(mysql_error());
    if(mysql_affected_rows() == 1)
      echo $id . "has been inserted </br>";
    } else{
    echo "This does not work </br>";
    }
    }
    
    Code (markup):
    Thanks
     
    thesportsman, Apr 29, 2011 IP
  3. lioncub5

    lioncub5 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try using the simpleXML functions:
    php-dot-net/manual/en/simplexml.examples-basic.php
    these should get you going
     
    lioncub5, Apr 29, 2011 IP