PHP

Import Text File to MySQL Database in PHP2 min read

In this article we will learn How to Import Text File to MySQL Database in PHP. In this example, PDO and SplFileObject classes are used to import Data to MySQL Database in php.




Step 1: Firstly,we create the data.txt file as follows

 

Step 2: Creating table. I created my table as customers and set id as auto increment.

 

Step 3: SplFileObject reads the text file line by line until reaches end of the text file. Each line contains person’s name, lastname and birth date. After reading line, we use list() function to use to assign values to a list of variables in one operation.

 

Step 4: Insert variables to database. After assign line to variable, PDO object improrts data to MySQL Database as follows.

 

Here is the answer of how to import Text File to MySQL Database. This examples works with PHP 5.1+ version.

 

 

 

 

About the author

admin

Leave a Comment