Help: How to import ";" denoted .txt file into a D

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

camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Help: How to import ";" denoted .txt file into a D

Post by camarosource »

I would like to import a file with info into my database.

Specs:

1. File is called rpo_list.txt
2. Located in same directory as import.php
3. Database is "camaro92_RPO"
4. Table is RPO_INFO, fields are "RPO_CODE" & "DESCRIPTION".

The contents of the file has 2 pieces of info for each line seperated by a ";".

ie.

L98;5.7-liter v8 350-cid engine for 3rd generation camaro's
LS1;5.7-liter v8 engine for 4th generation camaro's

When the script runs, it would look at the first line and see "L98". Would then import this into the RPO_CODE field of RPO_INFO table. It would then note the ";" which tells it that the next info is for the next field and so the "5.7-liter v8 350-cid engine for 3rd generation camaro's" would be imported into field "DESCRIPTION" of table "RPO_INFO".

It would then repeat for each line down the script.

Can anyone please help me? Perhaps they can write a small script that will do this? I have a list of over 4,100 RPO codes and need a way to import it.

Thank you sincerely for all your help.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Okay, had a quick bash

Code: Select all

<?
// Connect to DB
// Change "localhost" to your database MySQL hostname
$link = mysql_connect("localhost", "YOUR_USERNAME", "YOUR_PASSWORD") or die("Could not connect: " . mysql_error());
mysql_select_db("camaro92_RPO", $link)

($fp = fopen("rpo_list.txt", 'r')) or die ("Couldn't open the data file");
	
// Iterate through each line of the file
while ( ! feof($fp) ) {
	$line = fgets($fp, 1024);
		
	// remove any extra white space or carriage returns
	$line = chop($line);
		
	// spilt each line into individual fields
	$description = explode(";", $line);
	
	// build our query
	$query = "INSERT INTO RPO_INFO(RPO_CODE, DESCRIPTION) values('$description[0]', '$description[1]')";
	
	// execute the query
	mysql_query($query, $link) or die (mysql_error());

}

fclose($fp);
?>
Haven't trsted this, so might need a bit of tweaking.

Let me know any errors that occur and i'll try and help you out.

Mark
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

<?
// Connect to DB
// Change "localhost" your database path/source
$link = mysql_connect("localhost", "camaro92_RPO", "******") or die("Could not connect: " . mysql_error());
mysql_select_db("RPO_INFO", $link)

($fp = fopen("rpo_list.txt", 'r')) or die ("Couldn't open the data file");

// Iterate through each line of the file
while ( ! feof($fp) ) {
$line = fgets($fp, 1024);

// remove any extra white space or carriage returns
$line = chop($line);

// spilt each line into individual fields
$description = explode(";", $line);


$query = "INSERT INTO diary(RPO_CODE, DESCRIPTION) values('$description[0]', '$description[1]')";

mysql_query($query, $link) or die (mysql_error());

}

fclose($fp);
?>

Error on Line 7 ($fp = fopen("rpo_list.txt", 'r')) or die ("Couldn't open the data file");

Password conceild for obvious reasons
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

is the txt file in the same directory as the php file?

also, try copying the code i gave you again, cos i have edited it since you grabbed it! :D

Mark
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

Bech100 wrote:is the txt file in the same directory as the php file?

also, try copying the code i gave you again, cos i have edited it since you grabbed it! :

Mark
Re-copy pasted it. Same error.. Line 7
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

are you on a windows machine?

you may need to put the full path to the file

Code: Select all

($fp = fopen("c:\\path\\to\\the\\file\\rpo_list.txt", 'r')) or die ("Couldn't open the data file");
note the double slashes, you need to escape the backslash

Mark
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

Bech100 wrote:are you on a windows machine?

you may need to put the full path to the file

Code: Select all

($fp = fopen("c:\\path\\to\\the\\file\\rpo_list.txt", 'r')) or die ("Couldn't open the data file");
note the double slashes, you need to escape the backslash

Mark
My host uses UNIX. But I put the Absolute path and same error.

Here you can view it: http://camarosource.ca/php/rpo_decoder/ ... hpbb2.phps

And run the script http://camarosource.ca/php/rpo_decoder/ ... phpbb2.php
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this

Code: Select all

($fp = fopen("/home/camaro92/public_html/php/rpo_decoder/admin/rpo_list.txt", 'r')) or die ("Couldn't open the data file");
Mark
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

Bech100 wrote:try this

Code: Select all

($fp = fopen("/home/camaro92/public_html/php/rpo_decoder/admin/rpo_list.txt", 'r')) or die ("Couldn't open the data file");
Mark
same error
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

also, try double quote around the r instead of single quotes
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

Bech100 wrote:also, try double quote around the r instead of single quotes
same
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

has the php file got permission to read the txt file.

it should have, but i would just check.

The first code i gave you should work if the text file and ph file are in the same directory.

but try changing the line to

Code: Select all

$fp = fopen("rpo_list.txt", 'r') or die ("Couldn't open the data file");
Mark
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

permission set to 777
File exists in same directory
http://camarosource.ca/php/rpo_decoder/ ... o_list.txt
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

okay, figured it out

Get the original code and change the line that says

Code: Select all

mysql_select_db("camaro92_RPO", $link)
to

Code: Select all

mysql_select_db("camaro92_RPO", $link);

Can't believe i missed that, damn semi-colons!!!!!! :oops:

Mark
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

same
Post Reply