Re: Importing Data from Text File

From: Date: Wed, 18 Apr 2001 05:44:23 +0000
Subject: Re: Importing Data from Text File
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-49122@lists.php.net to get a copy of this message
Chris, What you need to do is read each line of the file into an array: $aryInput = file("/path/filename", "r"); Connect to the Database: $dbh = mysql_connect ("localhost", "user", "password"); if (! $dbh) { print "Error connecting to the Database Engine<br>"; } Select the table: mysql_select_db (stock); Now process each line in turn: for($i=0;$i<count($aryInput);$i++) { // Split each line into the fields you want $aryInputLine = explode(" ", $aryInputLine[$i]); // There is a TAB between the " " on the previous line and this assumes that there is only // one TAB between the fields $stock_id = $aryInputLine[0]; $stock_name = $aryInputLine[1]; $stock_desc = $aryInputLine[2]; $stock_length = $aryInputLine[3]; $stock_qty = $aryInputLine[4]; $stock_price = $aryInputLine[5]; // Now put your insert statement here $insstock = mysql_query ("INSERT INTO tablename (stock_name, stock_desc, stock_length, stock_qty, stock_price) VALUES ('$stock_name', '$stock_desc', $stock_length, $stock_qty, $stock_price)", $dbh); } This should get you started! Chris Fry Chris Aitken wrote: > Hi Everyone, > > Ive been asked to do a task here at my work and its something ive never > done before. I was hoping you guys could point me in the right direction so > I can research some more and discover how in the hell im gonna do this :) > > Basically, I have been issued a TAB delimited text file which I need to > process and insert all the data into a table. Here is a basic example of > what I need to do.......... > > Say the text file had the following lines (thousands of them, 1 record per > line) > > Screw Big 12 200 1.99 > Screw Big 10 400 1.50 > > And I wanted to import each line into a table (Mysql DB) that had 6 fields > (5 for each of the above plus an autoincremented ID field > > stock_id > stock_name > stock_desc > stock_length > stock_qty > stock_price > > Any suggestions and direction pointing will be greatly appreciated. > > -- > Chris Aitken - Webmaster/Database Designer - IDEAL Internet > email: chris@ideal.net.au phone: +61 2 4628 8888 fax: +61 2 4628 8890 > -------------------------------------------- > > Unix -- because a computer's a terrible thing to waste! > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net -- Chris Fry Quillsoft Pty Ltd Specialists in Secure Internet Services and E-Commerce Solutions 10 Gray Street Kogarah NSW 2217 Australia Phone: +61 2 9553 1691 Fax: +61 2 9553 1692 Mobile: 0419 414 323 eMail: chris@quillsoft.com.au http://www.quillsoft.com.au You can download our Public CA Certificate from:- https://ca.secureanywhere.com/htdocs/cacert.crt ********************************************************************** This information contains confidential information intended only for the use of the authorised recipient. If you are not an authorised recipient of this e-mail, please contact Quillsoft Pty Ltd by return e-mail. In this case, you should not read, print, re-transmit, store or act in reliance on this e-mail or any attachments, and should destroy all copies of them. This e-mail and any attachments may also contain copyright material belonging to Quillsoft Pty Ltd. The views expressed in this e-mail or attachments are the views of the author and not the views of Quillsoft Pty Ltd. You should only deal with the material contained in this e-mail if you are authorised to do so. This notice should not be removed.

« previous php.general (#49122) next »