SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing and Inserting a csv via Insert

    I have been trying to figure this out. From another post I got this which looks like what I need :

    You either need to create a custom PHP script to parse a tab (or whatever) delimited file and insert each row into the database, or you can use phpMyAdmin. I highly recommend using a custom script, since phpMyAdmin can be finicky at times.

    and the code

    $var*=*"1,2,3,4,6,6,7,8,9";



    $line*=*explode("\n",*$var);



    foreach($line*as*newline){

    ****$newline*=*explode(",",*$newline);

    ****$sql*=*"INSERT*INTO*table*SET*1='$newline[0]',*2='$newline[1]"

    ****if(!mysql_result($sql){

    ********echo("Database Error");

    ********exit();

    ****}

    }


    I dont understand the insert statement, specifically this

    1='$newline[0]',*2='$newline[1]"


    What does the [0] and the [1] stand for?

    How could I make this for exploding my file and importing into only one field. Its just a bunch of emails.

    Thanks for your help, I only have like a week with php and seemingly simple things like this not so obvious to me.



    cheers
    jp

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the [0] and [1] are the elements of the $newline array that was created by explode()'ing on the commas.

    how are your e-mails stored in the file? is each on a new line, comma seperated, or what? if they're on new lines, this should work:

    PHP Code:
    // file with e-mail addresses in it
    // file() returns an array of the lines in the file
    $emails file('./emails.txt');

    foreach (
    $emails as $email)
    {
        
    $email trim($email);
        
    mysql_query("INSERT INTO email_table SET email='$email'") or die(mysql_error());

    it could be made to INSERT all the e-mails in one query, rather than 1 query for each, but this is easy to understand.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks the names are on different lines so I will use your example.

    If you dont mind I have 2 questions about the exploding...just want to know in case I need it later

    1. The two elements , would that be the name and the comma?

    2. and is the first explode just to break them into seperate lines.

    thanks alot
    for your help.

    jp

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, the first explode() in the code you posted splits the data apart on new lines.

    about [0] and [1], say you had this code:

    PHP Code:
    $txt 'foo,bar';
    $array explode(','$txt); 
    $array[0] would be `foo' and $array[1] would be `bar'. understand?

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think so ...it would be the values which are on either side of the comma then


    Does it matter what number I put in the [ ]'s

    could it just as well have been any number or is it specifically have to be 0 and 1

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, you can't just specify any number. `explode('x', $var)' returns an array with elements numbered 0 through n, where n is amount of x in $var. does that make sense?



    EDIT: just fixed a tiny typo.
    Last edited by DR_LaRRY_PEpPeR; Apr 28, 2002 at 22:04.

  7. #7
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think so

    Im bookmarking this page and Im going to keep reading over what you said by tommorow it will make more sense. A little experimenting will clear it up completely


    Your help has been excellent. I can't beleive they dont explain that better in these books.


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
  •