Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    fgetcsv string delimiter issues

    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!");
    @mysql_select_db("listings")
    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
    recnum='$recnum',
    name='$name',
    empnum='$empnum',
    numyears='$numyears',
    address='$address',
    comment1='$comment1',
    comment2='$comment2'";

    @mysql_query($query);

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

    }

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •