View Full Version : fgetcsv string delimiter issues

06-14-2005, 02:27 AM
I have a data file emailed to me every week from a third party company. The fields within the txt file are delimited using a vertical bar '|'. Within each record of the txt file are 2 comment fields which can contain any ascii character.
I read a record from the txt file and load it into a mysql db.
When a comments field uses an '!' , it seems to be interpreted as a field delimiter character. Anytime the '!' is used within the comment fields the record does not load. This is the only difference between records loading and records not loading.
The program woks absolutely fine except when there is an '!' in the record.

Anyone know why an '!' would cause problems within this code?

Thanks in advance!!!

The code looks like the following:

@mysql_pconnect("localhost", "uname", "password")
or die("Could not connect to MySQL server!");
or die("Could not access the database, please try again later.");

/* Open the datafeed file */

$fh = fopen("MF-COM-load-data.txt", "r");

/* Load each field and load into database */

while ($data = fgetcsv($fh, 6000, "|"))

$recnum = $data[0];
$name = $data[1];
$empnum = $data[2];
$numyears = $data[3];
$numdepend = $data[4];
$numunits = $data[5];
$address = $data[6];
$comment1 = $data[7];
$comment2 = $data[8];

$query = "INSERT IGNORE INTO commercial SET


if (mysql_errno() != 0) {
echo "**** recnum=",$recnum," did not load, error = ".mysql_errno()," <br>";


06-14-2005, 08:07 AM
Try changing your while statement to look like this:

while(($data = fgetcsv($fh, 6000, "|")) != FALSE)

And see if that helps. If not, maybe you can first grab all the data into a string, convert any !'s into something else, like periods '.' and the put the string back into the file before running your fgetcsv routine.