SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict einSTein's Avatar
    Join Date
    Aug 2006
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    importing txt file content

    i want to import wa txt file contains 5000 mail to mysql dabase but i need an easy way not to insert the 5000 mail line by line

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Insert one record manually using phpmyadmin, and see what SQL statement is generated to enter the record. Then do a search and replace in your text file to make all the 5000 lines as per a similar structure. Copy and paste this code in Phpmyadmin->SQL

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    edge of nowhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have a CSV style formatting in your file it should be easy enough to load with LOAD DATA INFILE.
    And even if it's not, I'm sure you can craft a simple PHP script to reformat the text document line after line.
    Post us a line of your document, obscure the data, maybe we can give a hint.
    Programming boils down to three things: fast, good and cheap.
    Please pick two.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or, if you want, you could store every line in your file to an array, and use a foreach statement to loop through the emails and add the query to a string, for example:
    PHP Code:
    <?
    $email_string 
    file_get_contents("emails.txt");
    $email_array explode("\n"$email_string);
    $query_string "INSERT INTO `emails` (`email`) VALUES ";
    foreach(
    $email_array as $line => $email_address){
        if(
    $line 0){
            
    $query_string .= ", ";
        }
        
    $query_string .= "('".$email_address."')";
    }
    mysql_query($query_string);
    Or if you don't want to run the query, but just see it (to check it over), echo $query_string instead of querying it.
    Last edited by Jake Arkinstall; Aug 27, 2007 at 02:29. Reason: Forgot to put single quotes on adding email address to query
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    edge of nowhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOAD DATA INFILE is supposed to work a lot faster and be used for loading a large number of records in a DB, as opposed to inserting each one.
    At 5k records, say processing and inserting each and every one takes 1ms, and you still have a 5s processing time. Some hosters timeout their PHP scripts at 2s.
    Programming boils down to three things: fast, good and cheap.
    Please pick two.


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
  •