RE: [PHP] insert *.TAB into table
| From: | Boget, Chris | Date: | Tue, 01 Aug 2000 13:39:35 +0000 |
| Subject: | RE: [PHP] insert *.TAB into table | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-9430@lists.php.net to get a copy of this message | ||
> I'm using an *.tab file and importing like ;
> $fp = fopen ("$userfile","r");
> while ($data = fgetcsv($fp, 10000, " ")){
Well, if this is a tab delimited file, then the above should
be:
while ($data = fgetcsv($fp, 10000, "\t")){
More importantly, though, is that if you are having
problems with empty fields then fgetcsv() might not
be the way to do what you need. To determine if this
is the case, I'd probably change the above line in your
code to use tabs instead of spaces. If you are still
having the problem after that, then you should probably
change the approach entirely to accomodate the
possibility of empty fields. To do that, you can do
something like this:
$fileArray = file( $userfile );
for( $i = 0; $i < count( $fileArray ); $i++ ) {
// convert the double tabs to "tab space tab"
$curRec = eregi_replace( "\t\t", "\t \t", $fileArray[$i] );
// turn this into something we can use to insert
$curRec = eregi_replace( "\t", "\", \"", $curRec );
$dbQuery = "INSERT INTO mytable
( ... field list ... )
VALUES
( \"$curRec\" )";
$insertResult = mysql( $dbname, $dbQuery )
// do your query error checking here
}
The above is a quick and dirty example. What the
line
$curRec = eregi_replace( "\t", "\", \"", $curRec );
does is turn something like
name\taddress\temail\tphone\tfax
into
name", "address", "email", "phone", "fax
which you can use directly in your insert query. All
you need to do is add the beginning and end quotes
(so that mySQL doesn't yell and scream), which is
what I do in the actual query. You just need to make
sure that all of your fields match up.
Chris