Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How do I change a regular text file into a database??

    Hi,
    I have a text file formatted like this:

    884 225 LIGHTED ROSE BASKET 1 EA 29.95
    1402 168 3 PC BRASS BUTTERFLY WALL SET 1 SET 18.95

    Is it possible to input that into a MySQL database without doing it by hand? What is the PHP used to do this?

    Thanks!
    Luke Spragg

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to create a table with fields for each kind of data you have there.

    Then to insert the data, one way would be to write a PHP script to read a line from your file, pick out the different pieces of data, and insert them in to the database with the INSERT command.

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bulgaria
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HI,
    do You want to inser in the database the text or the file itself?

    Alex

  • #4
    New Coder
    Join Date
    Oct 2002
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to insert the text into the database without doing it one by one.
    Luke

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are they separated something like this:

    884 -- 225 -- LIGHTED ROSE BASKET -- 1 EA -- 29.95
    1402 -- 168 -- 3 PC BRASS BUTTERFLY WALL SET -- 1 SET -- 18.95

    ?

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bulgaria
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think You'll have to read the file line by line, but then You can concatenate everything in a string variable and insert it into a LONGTEXT field for example - it depends on how large the initial file is...

    Alex

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think X-MC wants to separate his data into rows and columns corresponding to the rows and data bits in the file.

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bulgaria
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Dylan,
    Where did You read this?
    wants to separate his data into rows and columns corresponding to the rows and data bits in the file
    It seems logical to do it like that, You are right, but it was not requested, or probably i missed some posts?

    Alex

  • #9
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is common thing to do, changing a file based system into a database one. Usually files are delimited with commas or some other character.
    I could be completely wrong though, he might want the entire file in the databse as one row.

  • #10
    New Coder
    Join Date
    Oct 2002
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to input the file I have into a mysql database with seperate columns and rows for each item.
    Luke

  • #11
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, are they separated like this?

    884 -- 225 -- LIGHTED ROSE BASKET -- 1 EA -- 29.95
    1402 -- 168 -- 3 PC BRASS BUTTERFLY WALL SET -- 1 SET -- 18.95

    ?

  • #12
    New Coder
    Join Date
    Oct 2002
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, there are just spaces like you saw in my example before...the spaces in the actual file are a lot larger though.

    884 225 LIGHTED ROSE BASKET 1 EA 29.95
    1402 168 3 PC BRASS BUTTERFLY WALL SET 1 SET 18.95
    Luke

  • #13
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, so your file is delimited with tabs? or multiple spaces? What separates the fields?

  • #14
    New Coder
    Join Date
    Oct 2002
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    multiple spaces no tabs
    Last edited by X-MC; 10-09-2002 at 09:32 PM.
    Luke

  • #15
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How many spaces?

    You could use the explode() function in PHP to turn one line from the file into an array of values, then implode() to format them for the database query.

    Post a sample of the file contents using the [ code ] tag in the forum here.
    Last edited by Dylan Leblanc; 10-09-2002 at 09:30 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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