SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello everyone!

    I have a bit of a problem. This is a .txt file that I have:

    122323123
    238212381
    327832981
    322873322
    232232332
    235454455
    455353534
    ...and so forth into oblivion

    What I would like to do is to smack all these into a MySQL database in some nifty manner. These are over 1000 numbers, and I will get new txt files all the time, so doing this manually is just not an option. (no, it's not CC numbers)

    /Mattias
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do this

    PHP Code:
    $data file("yourtextfile");
    foreach(
    $data as $key => $val) {
    $result mysql_query("INSERT into table set field = '$val'");
    unset(
    $result);

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And freddy saves the day!



    Thanks!
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freddy, the last statement in the code you provide is:
    PHP Code:
    unset($result); 
    What is the reason for this code. $result should be an int should it not? Is it that important to free up a couple of bytes of memory at this point in the script, or am I missing something?

  5. #5
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, darn - I was a bit too quick there. I have two problems.

    1. For some reason, it refuses to add more than exactely 127 rows. Very strange.

    2. If a row begins with a zero, it ignores the zero - can't have that!
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  6. #6
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh disregard my last post - It was I who accidentally put the ID table as tinyint - I made it mediumint, and it worked fine.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by truelight
    2. If a row begins with a zero, it ignores the zero - can't have that!
    If a field begins with a zero then it is not a number (to my way of thinking - it is an numeric code not a number) and I would be tempted to store it in a varchar field (but that's my C way of thinking coming through). That said, if you use a int type field, then defining the field with the ZEROFILL option may help?!?

  8. #8
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, ah - how stupid of me!

    /me changes field to varchar.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, more for general info (because you have probably already imported the data by now); in mysql you can use a LOAD DATA query to suck the contents of a text file into a table http://www.mysql.com/doc/L/O/LOAD_DATA.html

  10. #10
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice function, but I will probably not need it. Freddys method imported the file with 1000 rows in about 2 seconds.
    Last edited by M. Johansson; Apr 18, 2001 at 14:03.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com


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
  •