Re: Rewriting a textfile for import into a db..
| From: | Richard Lynch | Date: | Thu, 09 Aug 2001 20:50:28 +0000 |
| Subject: | Re: Rewriting a textfile for import into a db.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-62072@lists.php.net to get a copy of this message | ||
> 123|blah|foo|bar|etc
>
> To be formatted like:
>
> INSERT INTO Table VALUES
> (123,'blah','foo','bar','etc');
Assuming there are not | in the actual data:
<?php
$path = "/path/to/file/to/import.txt";
# A few thousand entries is a bit much to suck in at once, but
# it's a one-time hack, so who cares?
$file = file($path) or die("Could not load file");
while (list(,$line) = each($file)){
$values = explode('|', $line);
$query = "insert into into table values($values[0], '$values[1]',
'$values[2]'...";
mysql_query($query) or die(mysql_error());
}
?>
You may want to add a line counter and output that with mysql_error() so you
can quickly refer back to your data file to find a broken line...
--
WARNING richard@zend.com address is an endangered species -- Use
ceo@l-i-e.com
Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm