Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing a txt file to MySQL Database using PHP script 1

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hi,

I found this script to import a txt to a MySQL table. In my case, the table is called sperr001, and the txt file is called HSD.txt with CSV format (under c:\php).

<?php

include 'db_connection.php';
include 'header.htm';

## CSV file to read in ##
$CSVFile = 'C:\\php\\HSD.txt';

mysql_query('LOAD DATA LOCAL INFILE 'C:\\php\\HSD.txt' INTO TABLE sperr001 FIELDS TERMINATED BY "," LINES TERMINATED BY "\\r\\n";') or die('Error loading data file.<br>' . mysql_error());

## Close database connection when finished ##
mysql_close($conn);
?>

After I run this code, my browser went blank (no message at all) and my DB table was not updated with the data in the txt file.

What did I do wrong?

Should I or should I not include the directory of the file? When I don't include it, it says error file not found.

Please help!!
 
Your browser will be blank, as your script appears to not to output anything.

I don't see how your query could work and I'm suprised you're not getting an error. You have problems with quotes:

string begins: '
LOAD DATA LOCAL INFILE
string ends: '
bare text: C:\\php\\HSD.txt
string begins: '
INTO TABLE sperr001 FIELDS TERMINATED BY "," LINES TERMINATED BY "\\r\\n";
string ends: ') or die('Error loading data file.<br>' . mysql_error());

I generally advise putting MySQL queries inside doublequotes as one tends to use singlequotes to delineate strings:


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
why not write your won script that opens the CSV file, reads it one line at a time and then splits that data and then writes that to the database? This method also has the advantage of allowing you to filter out nasty characters such as quotes in the CSV file and you have more control as to how the data is entered into the DB

 
mes123:
I don't know aas1161's answer, but my answer to the question would be, "Because using LOAD DATA is faster".


aas1161:
Drat! There's something I missed. You are using "LOAD DATA LOCAL INFILE". Don't use "LOCAL", only "LOAD DATA INFILE", it's only used in those cases where you are going to transmit the file to the MySQL server though the database connection. MySQL, last time I checked, does not ship with that turned on by default.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
to sleipnir214:
I would try without LOCAL. I couldn't try that right now because my work is in my office and now it's Sunday! :)

And, should I include the directory then, where the file is?

to mes123:
My PHP knowledge is not that good, since I just started. That's why I looked for scripts. If you could tell me how to get it done, I would be very thankful, while I'm learning the language now.

Thanks...

I will get back to you guys on Monday!!
 
ok, below are the important bits from a similar script I wrote. You'll have to modify bits of it since my csv file will be organised different than yours.


Code:
// open database connection

$conn = mysql_connect("localhost", "$user", "$pass");
echo "Connected to database : $conn<br>";
mysql_select_db("nameofdb", $conn);

// open csv file

$filename = "mycvsfile.csv";
$fp = fopen($filename, "r") or die("Failed to open flat file");
echo "Opened flat file ok...<br>";
flock($fp, LOCK_EX); // lock the file while we got it open

// the first line of the csv usually contains
// the descriptions of the columns. We don't want
// to write this into the db so set a flag
$firstline = true;

// now we read each line and write that to the database
while (!feof($fp)){
  if ($firstline) {
    // skip this iteration of the while loop
    // and carry on with next line
    $firstline = false;
    continue;
  }

  // read this line
  $line = fgets($fp,16384);

  // remove any ' and replace with `
  $line = str_replace("'", "`", $line);
  
  // next split this line into it's componates
  $delim = ",";
  $data = explode($delim, $line);
  // now $data will be an array contining
  // all the fields. eg the first field will be
  // $data[0] and the second $data[1] etc
  // do any processing you want with these now
  
    
  // now we have all the luvly data ...
  $sql = "insert into tablename values (";
  $sql .= $data[0] . ", " ;  // a numberic value
  $sql .= "'" . $data[1] . "', "; // a string
  $sql .= "'" . $data[2] . "')"; // the last value

  // remember that string value must be enclosed in
  //  quotes ' and numeric don't


  echo "Inserting item: <br> ";
  $result = mysql_query($sql, $conn) or die (mysql_error());
   
 }

echo "all done we hope! <br>";

mysql_close($conn); // close db connection
flock($fp, LOCK_UN); // unlock the file

Hope this helps.

Any problems let me know.







 
mes123: Thanks for the code. I'll keep it for reference.

But for my current problem, I've solved it with LOAD DATA (thanks sleipnir214!).

Still related to "import" problem, do you guys know by any chance how to insert column with AUTO_INCREMENT option while this column doesn't exist in a txt file but exists in the database table?

In my case, under HSD.txt there are only 4 fields: SG, BLZ, ACCOUNT, NOTE. I want to import these data into a table that has an additional field, called ID. Like usual, ID is primary key with AUTO_INCREMENT.

Any idea?
 
IAW you must specify the columns to which the data should be loaded, and specifically exclude the auto_increment column's name from the list of columns.


Something like:

LOAD DATA LOCAL 'C:\php\HSD.txt'
INTO TABLE sperr001
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(SG, BLZ, ACCOUNT, NOTE)


Want the best answers? Ask the best questions!

TANSTAAFL!!
 

I've got it now. Thanks a lot! You deserve a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top