PHP reads TXT file into database

Source: Internet
Author: User
Tags fread
There is a TXT file, about 300 MB, want to read PHP, and then the sub-bar inserted into the database, what should be done, the TXT file format is probably the case. What should I do?

Wangli # 20100203 # John Doe
LIWSI # 2015544 # Xiaofen
Wangli # 20100203 # John Doe
LIWSI # 2015544 # Xiaofen
Wangli # 20100203 # John Doe
LIWSI # 2015544 # Xiaofen
Wangli # 20100203 # John Doe
LIWSI # 2015544 # Xiaofen
Wangli # 20100203 # John Doe
LIWSI # 2015544 # Xiaofen
Wangli # 20100203 # John Doe
LIWSI # 2015544 # Xiaofen


This is my own writing, but just can read out and display except, do not know how to insert the database, and TXT file is originally there is a line, but PHP read out there is a newline, there is a problem is if using fread ($data, 1000) This place is 1000 refers to reading 1000 characters, if you need a line of reading it. Now this txt is a line like this, if you say 1000 characters this way, it is possible to finally just read to half a row

 
  ';


Reply to discussion (solution)

$data = ' 11.txt '; $fp = fopen ($data, ' R '), while ($r = fgets ($fp)) {  $t = join ("', '", Explode (' # ', $r));  $sql = "INSERT into table values (' $t ')";  //.... Execute SQL}


However, it seems that MySQL should use the LOAD DATA INFILE command to complete

PHP code?1234567$data = ' 11.txt '; $fp = fopen ($data, ' R '), while ($r = fgets ($fp)) {$t = join ("', '", Explode (' # ', $r)); $s QL = "INSERT into table values (' $t ')"; //.... Execute SQL}

But for mysq ...

I follow your this, and then open this page, directly do not show, blank ~

$data = ' 11.txt '; $fp = fopen ($data, ' R '), while ($r = fgets ($fp)) {echo $t = join ("', '", Explode (' # ', $r));  $sql = "INSERT into table values (' $t ')";  //.... Execute SQL}




# is not inserted into the database, in addition, if the file is large, how to split it

How to output to a line of PHP to display, txt default newline is \ n, so, PHP how to match out

Is your data not worth the row by column ("#")?
Only if you provide the right data and needs, can you get a workable solution

$filename = "11.txt"; $fps =fopen ($filename, ' R '), while (!feof ($fps)) {$ceshi =fgets ($fps); Echo $ceshi. "
";} Fclose ($fps);


Thank you moderator, I follow this, and then the browser output is and txt text as shown, but if you want to insert into the database what should be done?

I have four fields in my database, Id,name,time,bak.

The first one is ID is auto_increment
All three of the remaining fields are varchar
Now the TXT file is in the format of
Lisi # 121015 # 3900
Xiaomao # 130205 #2500
Lisa # 1258 # OK
I want to insert into the data, the data is like this.
ID Name Time Bak
1 Lisi 121015 3900
2 Xiaomao 130205 2500
3 Lisa 1258 OK




I'm not mistaken, the column delimiter is "#", that is, there is a space on each side of the # number (really hard for you, don't wearies?) )
Fill in the field list to
$t = Join ("', '", Explode (' # ', $r));
$sql = "INSERT into table (name, time, bak) values (' $t ')";

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.