Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: how to read file and insert database

  1. #1
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool how to read file and insert database

    dunno where else to put this
    how to read the contents of a file and insert it into a database?
    tnx
    Please don't mind me. I am just posting a lot of nonsense.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    mysql_query('insert into tablename (columnname) values ("' .   mysql_real_escape_string(file_get_contents('somefile')) . '");');
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    tnx. how about database results save to file?
    Please don't mind me. I am just posting a lot of nonsense.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $a = mysql_query('some query or other');
    $b = fopen('somefile', 'a');
    $c = mysql_fetch_array($a, MYSQL_ASSOC);
    $d = array();
    foreach($c as $e => $f)
      array_push($f, $e);
    
    fwrite($b, implode("\t", $d) . "\n");
    do {
      fwrite($b, implode("\t", $c) . "\n");
    } while($c = mysql_fetch_array($a));
    fclose($b);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thnx
    Please don't mind me. I am just posting a lot of nonsense.

  6. #6
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    follow up question what's the difference between include_once() and require_once() in terms of usage? read the php.net manual. need a layperson's explanations. tnx
    Please don't mind me. I am just posting a lot of nonsense.

  7. #7
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Code:
    mysql_query('insert into tablename (columnname) values ("' .   mysql_real_escape_string(file_get_contents('somefile')) . '");');
    the code works only reading one line of text
    is there such a thing as while not EOF { do something }
    i want something like:
    Code:
    first line of text
    second line of text
    then in the database:
    Code:
    id                name
    1                first line of text
    2                second line of text
    p.s. sorry for not making myself clear on the first post tnx.
    Please don't mind me. I am just posting a lot of nonsense.

  8. #8
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    include_once issues a warning if it fails. require_once issues an error and stops the execution of the script.

    As for the multiple lines
    PHP Code:
    <?php
    $lines 
    file('somefile.txt');
    foreach(
    $lines as $num => $line){
    mysql_query('insert into tablename (columnname) values ("' .   mysql_real_escape_string($line) . '");');
    }
    ?>
    Then just make sure your table has a column with auto incretment

  9. #9
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by blm126
    include_once issues a warning if it fails. require_once issues an error and stops the execution of the script.

    As for the multiple lines
    PHP Code:
    <?php
    $lines 
    file('somefile.txt');
    foreach(
    $lines as $num => $line){
    mysql_query('insert into tablename (columnname) values ("' .   mysql_real_escape_string($line) . '");');
    }
    ?>
    Then just make sure your table has a column with auto incretment
    no data is being inserted.
    Please don't mind me. I am just posting a lot of nonsense.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    the code works only reading one line of text
    is there such a thing as while not EOF { do something }
    How peculiar. There is, but it shouldn't be necessary here. Try:
    Code:
    mysql_query('insert into tablename (columnname) values ("' .   mysql_real_escape_string(str_replace("\n", '\n', file_get_contents('somefile'))) . '");');
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •