PHP reads TXT data and inserts it into the database

Source: Internet
Author: User
PHP reads the TXT file and inserts it into the database. there is a TXT file, which is about 300 MB or more. what should I do if I want PHP to read it and insert it into the database in different entries? the TXT file format is like this. What should I do? wangli & nbsp; # & nbsp; 20100203 & nbsp; # & nbsp; Li Si liwsi & nbsp; # & nbsp; 201 PHP reads TXT files and inserts them into the database
There is a TXT file, which is about 300 MB or more. what should I do if I want PHP to read the file and insert it into the database in multiple entries? the TXT file format is like this. What should I do?

Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen
Wangli #20100203 # Li Si
Liwsi #2015544 # xiaofen


I wrote this by myself, but it can only be read and retrieved and displayed, except that I don't know how to insert the database, and there is a line break in the TXT file, but there is a line break when PHP reads it, another problem is that if fread ($ data, 1000) is used, where 1000 refers to reading 1000 characters, what if one row and one row are needed. Now this TXT is like a row. if the TXT contains 1000 characters, it is possible that it only reads half a row.

 $data='11.txt';
$files=fopen($data,"r");
$res=fread($files,100000);
echo $res.'
';

------ 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 be completed using the load data infile command.

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.