Results 1 to 7 of 7
  1. #1
    Full Member
    Join Date
    January 18th, 2005
    Posts
    270
    Okay, I am stumbling my way thru PHP/SQL.

    I have created a table in my DB. I created a PHP file to go out and download a file into my root directory of my server via FTP.

    Now I am trying to load this file into the table using PHP. There has to be a way to do this, but for the life of me, I cannot figure out the command. (yes I know how to do it using PHPMyAdmin, but I want to do it in my PHP program)

    Can any of you gurus help me out?

    Bsnrjones

    Goal: $10,000/month by 12/04

  2. #2
    ABW Ambassador Greg Rice's Avatar
    Join Date
    January 18th, 2005
    Location
    Ohio
    Posts
    4,889
    I use phpMyAdmin for mine but can't you use mySQL syntax?

    LOAD DATA INFILE 'datafile.txt' INTO TABLE `table_name` FIELDS TERMINATED BY '|' LINES TERMINATED BY '\r\n'

    something like that?

  3. #3
    Full Member
    Join Date
    January 18th, 2005
    Posts
    270
    Yes you can, but for some reason it is not working. I have the file in the same directory as the PHP script, but I am not sure it is actually finding it. It gives some error like it is not allowed by XXXX@localhost.

    Bsnrjones

    Goal: $10,000/month by 12/04

  4. #4
    Newbie
    Join Date
    January 18th, 2005
    Location
    Mount Pleasant, WI
    Posts
    41
    Bsnrjones,

    Let me admit that I am still new to PHP and MySQL, but the following works for me:

    <?
    $handle = fopen('file.txt', 'r');
    while ($data = fgetcsv ($handle, 9999, "|")) // Pipe is for pipe delineated
    {
    foreach ($data as $key => $value) $data[$key] = addslashes($data[$key]);
    $query = "INSERT INTO tablename(`product_name`, `short_desc`, `description`, `sku`, `url`, `thumb`, `large_image`, `price`, `retail`, `category`, `manufacturer`)
    VALUES('$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[9]', '$data[10]')";
    $result = mysql_query($query);
    }
    fclose ($handle);
    ?>

    I've found most datafeeds have the first line as the column names, so I'll use an if statement for the query, that way I don't have to manually go in and delete it. My goal is to make all datafeed inserts such that I don't have to do anything manually, and so far, so good.

    I will also admit that some of the PHP gurus here could find a way to do all that in one line... And for troubleshooting, you may want to throw in some error checking.

    Hope that helps!
    Good luck.
    ScottyB

    [This message was edited by ScottyB on January 31, 2004 at 12:24 AM.]

  5. #5
    Full Member
    Join Date
    January 18th, 2005
    Posts
    270
    Thanks ScottyB! I tweaked the category names and it worked great! Now I just have to wait for my cronjob to reset and I will have automatic updates!

    Thanks a bunch...

    Bsnrjones

    Goal: $10,000/month by 12/04

  6. #6
    Full Member
    Join Date
    January 18th, 2005
    Posts
    270
    Wow! I am so pleased with myself. I now have a php script the will automatically update my inventory in my OsCommerce shopping cart. Thanks for all you help guys!

    Now, I need to get to work using this script to automatically update my datafeed affiliate sites.

    Bsnrjones

    Goal: $10,000/month by 12/04

  7. #7
    Newbie
    Join Date
    January 18th, 2005
    Location
    Mount Pleasant, WI
    Posts
    41
    Bsnrjones,

    Glad to hear you got it working. I had the same problem with the "localhost" error -- it seems the "load data infile" using a script may be some sort of security risk... so many hosts disallow it.

    Glad to help!
    ScottyB

  8. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Is Popshops loading for you?
    By sassyz in forum PopShops
    Replies: 2
    Last Post: July 14th, 2011, 01:39 PM
  2. not loading
    By peterbndrs in forum ShareASale - SAS
    Replies: 2
    Last Post: August 17th, 2008, 12:37 AM
  3. zip file datafeed file transfer into database
    By rahulcbr in forum Programming / Datafeeds / Tools
    Replies: 13
    Last Post: August 28th, 2007, 04:59 AM
  4. File Structure and File Names...
    By tfmurphhk in forum WebMerge (Fourthworld.com)
    Replies: 4
    Last Post: March 2nd, 2005, 12:51 PM
  5. Incorporating the File No. in the Index File?
    By huhu in forum WebMerge (Fourthworld.com)
    Replies: 6
    Last Post: October 6th, 2003, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •