해외축구중계스포츠중계축구중계EPL중계스포츠무료중계실시간스포츠
PHP read text file and insert into MySQL database - WDB24
Site icon WDB24

PHP read text file and insert into MySQL database

php featured image

Today I was working on an in house project and suddenly one of my colleagues came to me with a text file and said please import this file into database. I did that for him.

At a same time I got an idea why not this small task should share with my PHP beginners. So in this tutorial I am going to show how to read text file and insert into mysql database.

Read text file and insert into MySQL database

First let me show you some of the built-in php functions which I am going to use in this process.


Create Database:

Create Database Table:

Text file content:

Database Connection:

Read text file and insert data:

In the above code first I open a employee-data.txt using reading (r) mode. Then I used while loop using feof() function and make sure loop must be iterated till the end of file. Then I stored file line in the $getTexLine variable and then convert that line into an array by using comma. I already knew that we have 4 type of data on each line so I used list() function and create 4 variables which are $name, $city, $postcode, $job_title. After that I create a mysql insert statement and add data into table. This process will continue until employee-data.txt content ends.

Also read:

Exit mobile version