Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: how to read file and insert database

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

    Default

    just like this?
    PHP Code:
    <?php
    $conn 
    mysql_connect('XXXX''XXXX''XXXX') or die ('Error connecting to mysql');
    mysql_select_db('XXXX'$conn) or die ('Error connecting to database');
    mysql_query('insert into texter (name) values ("' .   mysql_real_escape_string(str_replace("\n"'\n'file_get_contents('text.txt'))) . '");');
    ?>
    still no data is stored.

    MySQL database Dump
    Code:
    CREATE TABLE `texter` (
      `id` smallint(5) unsigned zerofill NOT NULL auto_increment,
      `name` varchar(150) collate latin1_bin NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin AUTO_INCREMENT=1 ;
    Last edited by jr_yeo; 08-09-2006 at 12:49 AM.
    Please don't mind me. I am just posting a lot of nonsense.

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

    Default

    guyz.... any ideas?
    Please don't mind me. I am just posting a lot of nonsense.

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

    Default

    You realise you've only allowed for 150 bytes of data there? If the file is more than 150 bytes, there will be problems. If not, it's time for debugging code to go in.
    Code:
    <?php
    $conn = mysql_connect('XXXX', 'XXXX', 'XXXX') or die ('Error connecting to mysql');
    mysql_select_db('XXXX', $conn) or die ('Error connecting to database');
    $query = 'insert into texter (name) values ("' .   mysql_real_escape_string(str_replace("\n", '\n', file_get_contents('text.txt'))) . '");'
    mysql_query($query) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
    ?>
    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!

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

    Default

    so far, got the impression that u want me to use BLOB here to insert data from file, right?
    but what m trying to achieve here is read a file, insert data to the database line by line, and output back the data thru tables and sort the "outputted" data?
    Please don't mind me. I am just posting a lot of nonsense.

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

    Default

    Oh, I see. You want each line to be a separate record?
    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!

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

    Default

    Quote Originally Posted by Twey
    Oh, I see. You want each line to be a separate record?
    yeah. i think its something to do with explode() or is it implode()? dunno much about php to read php.net manual efficiently
    Please don't mind me. I am just posting a lot of nonsense.

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

    Default

    You could use explode(), but in this case it's easier to simply use file() to read the file. This returns an array of the lines in the file.
    Code:
    <?php
    $conn = mysql_connect('XXXX', 'XXXX', 'XXXX') or die ('Error connecting to mysql');
    mysql_select_db('XXXX', $conn) or die ('Error connecting to database');
    $query = 'insert into texter (name) values ("%s");';
    $lines = file('text.txt');
    for($i = 0; $i < count($lines); ++$i)
      mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
    ?>
    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!

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

    Default

    will try later
    Please don't mind me. I am just posting a lot of nonsense.

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
  •