SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Really need help with importing text file

    Here is what I'm trying to do.
    Table A contains 'X' number of fields and my text file contain 'X' number of fields. I want to import the data in the text file to the records that it matches in Table A (3 fields in the text file = 3 fields in the table), and for the records that have no match, it will add a new record to the table. How can I do this with php or mysql.
    Thanks for your help.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a pseudo-code solution (I assume you're familiar with PHP to some degree - if not please post again and I'll attempt a fuller reply, although I know very little PHP). If I've misunderstood anything, please correct me.

    I assume that your text file has a number of fields (more than 3), and that you want to look at each record in it in turn; if there's a corresponding record in MySQL (where the 3 fields are the same) you want to update that record, if not you want to insert a new one?

    The program flow would be like this:

    1) Open the text file and a connection to the database.

    2) Start a loop. Each pass through the loop will take 1 record from the text file, analyse it, and update or insert to the database.

    3) Inside the loop, read the record from the text file into some variables, an array or whatever

    4) Construct an SQL statement which will check if this record exists in the database. You way you're checking three strings, so this will be along the lines of
    PHP Code:
    strSQL "select * from TABLE_NAME where FIELD_1 = '" strFirstFieldFromTextFile "' and FIELD_2 = '" strSecondFieldFromTextFile "' and FIELD_3 = '" strThirdFieldFromTextFile "'" 
    - obviously that's assuming they're all strings, omit the appropriate speech marks if any are integers etc.

    5) Execute the SQL statement and check if it returns any results. If it does, you need to update the record. If not, you need to add a new one.

    6) Construct the appropriate update or insert SQL statement using a similar technique to the above.

    7) Execute the SQL statement

    8) Move to the next line in the file, and move to the start of the loop.



    That will do the job - depending on how often it will run etc. it could probably be made more efficient, but if this is a one-off run to populate a DB with initial data that method should work fine.

    Disclaimer: I know little about PHP, so if someone comes along and says "hey there's this function in PHP that does all this automatically", don't laugh at me for being ignorant... . And please, if someone has a better/easier way of doing this, feel free to share it - there's bound to be loads .

  3. #3
    SitePoint Member
    Join Date
    Jan 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks CrazyCrane
    I will give that a try and see if it works. I've spents weeks on this. I'm just learning php so it's taking a while
    Thanks again


Bookmarks

Posting Permissions

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