Help: How to import ";" denoted .txt file into a D
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
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.
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.
Okay, had a quick bash
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
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);
?>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
<?
// 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
// 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
-
camarosource
- Forum Commoner
- Posts: 77
- Joined: Sat Aug 03, 2002 10:43 pm
are you on a windows machine?
you may need to put the full path to the file
note the double slashes, you need to escape the backslash
Mark
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");Mark
-
camarosource
- Forum Commoner
- Posts: 77
- Joined: Sat Aug 03, 2002 10:43 pm
My host uses UNIX. But I put the Absolute path and same error.Bech100 wrote:are you on a windows machine?
you may need to put the full path to the file
note the double slashes, you need to escape the backslashCode: Select all
($fp = fopen("c:\\path\\to\\the\\file\\rpo_list.txt", 'r')) or die ("Couldn't open the data file");
Mark
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
try this
Mark
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");-
camarosource
- Forum Commoner
- Posts: 77
- Joined: Sat Aug 03, 2002 10:43 pm
same errorBech100 wrote:try this
MarkCode: Select all
($fp = fopen("/home/camaro92/public_html/php/rpo_decoder/admin/rpo_list.txt", 'r')) or die ("Couldn't open the data file");
-
camarosource
- Forum Commoner
- Posts: 77
- Joined: Sat Aug 03, 2002 10:43 pm
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
Mark
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");-
camarosource
- Forum Commoner
- Posts: 77
- Joined: Sat Aug 03, 2002 10:43 pm
permission set to 777
File exists in same directory
http://camarosource.ca/php/rpo_decoder/ ... o_list.txt
File exists in same directory
http://camarosource.ca/php/rpo_decoder/ ... o_list.txt
okay, figured it out
Get the original code and change the line that says
to
Can't believe i missed that, damn semi-colons!!!!!!
Mark
Get the original code and change the line that says
Code: Select all
mysql_select_db("camaro92_RPO", $link)Code: Select all
mysql_select_db("camaro92_RPO", $link);Can't believe i missed that, damn semi-colons!!!!!!
Mark