Inserting data from text file into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Inserting data from text file into database

Post by nishmgopal »

Hi Guys

Say is I had 3 tables: AREA, UNIT and POSITION

How can I map the data from a textfile into that database? The textfile contains the data in this format:

REAL/IMT GENERATE//
MSGID/SHIPREPORT/01/SSEL/01-01-2000//

SUMM/SHIP ACTIVITY/ATLANTIC SEA//
MSIT/DA/FSH/BLUE WHALE/2323N3-09032E2/051545Z0SEP/-//
MSIT/DA/TUW/BLUE FACTORY/2323N3-09132E2/051545Z0SEP/-//

SUMM/SHIP ACTIVITY/BALTIC SEA//
MSIT/CA/FR/INSPECTION 1/PLACE:OSLO/120000Z0SEP/-//
MSIT/DA/HS/VIKING 2/PLACE:ESBJERG/110000Z0SEP/-//
MSIT/DA/FY/FLYVEFISKEN/PLACE:VEJLE FJORD/060930Z0SEP/-//
MSIT/NO/HS/VIKING/1323N3-10132E2/051050Z0SEP/-//
MSIT/DA/TUW/MOBY DICK/PLACE:SVANEKE/041000Z0SEP/-//
MSIT/DA/FR/HMS ULTRA/PLACE:HELSINKI/041000Z0SEP/-//
MSIT/SW/FSH/SILD I LANGE BANER/PLACE:STOCKHOLM/211200Z0SEP/-//
jh_1981
Forum Newbie
Posts: 22
Joined: Fri Jan 30, 2009 6:21 pm

Re: Inserting data from text file into database

Post by jh_1981 »

<?
$filepath="text.txt";
$fp= fopen ($filepath,"r");
while (!feof($fp)) // Loop til end of file.
{
$buffer = trim(fgets($fp, 4096)); // Read a line.
$sql="insert into table(name)values('".$buffer."')";
$db->query($sql);
//echo $buffer;
//echo "<br>";
}
echo "ok";
fclose ($fp);
?>
jh_1981
Forum Newbie
Posts: 22
Joined: Fri Jan 30, 2009 6:21 pm

Re: Inserting data from text file into database

Post by jh_1981 »

<?
$filepath="text.txt";
$fp= fopen ($filepath,"r");
while (!feof($fp)) // Loop til end of file.
{
$buffer = trim(fgets($fp, 4096)); // Read a line.
$sql="insert into table(name)values('".$buffer."')";
$db->query($sql);
//echo $buffer;
//echo "<br>";
}
echo "ok";
fclose ($fp);
?>
Post Reply