Jump to content

How to export values from an txt file?


chanchelkumar

Recommended Posts

Hi all,

 

I am trying with some data, which i get in a txt file separated with coma (,) .

i want this to insert into the database. the data is bulk... means it contains some 100 lines..

so i want to read the file and insert it to the database..

 

please help me...

 

thanks in advance...

 

 

Link to comment
https://forums.phpfreaks.com/topic/91658-how-to-export-values-from-an-txt-file/
Share on other sites

$file = file_get_contents("your_text_file.txt");
$contents = explode(",", $file); // Creates an array of values

for($i=0; $i<count($contents); $i++) {

// Use your mysql_query() here to insert the value of $contents[$i] to the database

}

 

Although that'll only work if you have 1 column of data within the text file you're wanting to insert.

just a quick note: in FOR loops any function within the parameters (eg count()) get executed on every loop, it would be less intensive if you saved the count to a variable, eg:

 

$x = count($contents);

for($i=0; $i<$x; $i++) {

// Use your mysql_query() here to insert the value of $contents[$i] to the database

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.