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
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.

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.