Page 1 of 2
Help: How to import ";" denoted .txt file into a D
Posted: Thu Aug 14, 2003 10:26 am
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.
Posted: Thu Aug 14, 2003 10:43 am
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
Posted: Thu Aug 14, 2003 10:55 am
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
Posted: Thu Aug 14, 2003 10:59 am
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!
Mark
Posted: Thu Aug 14, 2003 11:04 am
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
Posted: Thu Aug 14, 2003 11:06 am
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
Posted: Thu Aug 14, 2003 11:14 am
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
Posted: Thu Aug 14, 2003 11:18 am
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
Posted: Thu Aug 14, 2003 11:20 am
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
Posted: Thu Aug 14, 2003 11:21 am
by JayBird
also, try double quote around the r instead of single quotes
Posted: Thu Aug 14, 2003 11:22 am
by camarosource
Bech100 wrote:also, try double quote around the r instead of single quotes
same
Posted: Thu Aug 14, 2003 11:25 am
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
Posted: Thu Aug 14, 2003 11:27 am
by camarosource
Posted: Thu Aug 14, 2003 11:28 am
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!!!!!!
Mark
Posted: Thu Aug 14, 2003 11:30 am
by camarosource
same