Jump to content

Problem with inserting field into mysql using fgetcsv


shane2006

Recommended Posts

Headbanger here.  We download csv files (telephone records), parse them, do a little manipulation and insert them into a mysql database. We need to insert data into the database that does not exist in the csv files. The new data is simply a concat of text and an existing field. In mysql, it would be a simple command - concat("V2_",field0). However, when we try to do it in the php script, it fails with a "Check the manual . . . for the right syntax". The script works fine without the language marked by **language**. Is there a prohibition that we are unaware of? We have tried to insert the concat command into the mysql query, but that does not work either. Any other ideas?

 

while (($data = fgetcsv($handle, 1500, ";")) !== FALSE) {

$num = count($data);

  $row++;

  for ($i=0; $i < $num; $i++) {

  if($data[0]) {

if($data[7] == "NA") {

$data[7] = $data[7];

} else {

$data[7] = strtotime($data[7]);

$data[7] = date("Y-m-d H:i:s", $data[7]);

}

$data[6] = strtotime($data[6]);

$data[8] = strtotime($data[8]);

$data[6] = date("Y-m-d H:i:s", $data[6]);

$data[8] = date("Y-m-d H:i:s", $data[8]);

**$rsntwo = "V2_" . $data[0];**

 

$Query = "INSERT IGNORE INTO  $Table

(RecordSequenceNumber, RSN2,ConnectionType, SessionID, ReleaseCause, Start_TOD, Answer_TOD, Release_TOD, ReleaseCauseFromStack, BinaryReleaseCause. . .)

 

VALUES ('$data[0]', **'$rsntwo',** '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[10]', . . .)";

mysql_query($Query) or die(mysql_error());

  }

  }

}

fclose($handle);

                                                unlink("$LocalDirectory/$f");

}

}

 

Thanks all!

Link to comment
Share on other sites

update -

After researching a bit more, I discovered that I needed to add a temp array in order to add in the additional variable. Now if I can just stop it from iterating through each entry 28 or more times, I will have it beat!

 

Here it is with the temp array added:

 

$data[6] = strtotime($data[6]);

$data[8] = strtotime($data[8]);

$data[6] = date("Y-m-d H:i:s", $data[6]);

$data[8] = date("Y-m-d H:i:s", $data[8]);

$rsntwo = "V2_" . $data[0];

$temp = array($data[0], $data[3], $data[4], $data[5], $data[6],. . .);

$temp[] = $rsntwo;

$Query = "INSERT IGNORE INTO  $Table

(RecordSequenceNumber, RSN2, ConnectionType, SessionID, ReleaseCause, Start_TOD, Answer_TOD, Release_TOD, ReleaseCauseFromStack, . . .)

VALUES ('$data[0]', '$rsntwo','$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[10]', '$data[11]', '$data[12]', '$data[13]', '$data[14]', '$data[15]', '$data[16]', '$data[17]', '$data[18]', . . .')";

mysql_query($Query) or die(mysql_error());

}

}

}

fclose($handle);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.