I found somewhere a few lines of code that will do a basic convert from a CSV file into an SQL statement.
I'm having a bit of a problem with it when a particular field has commas inside it. It seems to recognize the commas somewhere in this method, but I don't quite understand how it's doing it. Here's an example of what's happening:
Original value:
New value:
here's the code:
i'm guessing my issue is somewhere on this line:
What would be nice is if it replaced those commas with semi-colons and got rid of the double quotes, like this:
Original value:
New value:
or maybe included those commas and just got rid of the double quotes, i don't know, but this line is causing errors in my SQL statement.
Thanks.
I'm having a bit of a problem with it when a particular field has commas inside it. It seems to recognize the commas somewhere in this method, but I don't quite understand how it's doing it. Here's an example of what's happening:
Original value:
Code:
"Spanish, French, Romanian"
Code:
'\"Spanish', ' French', ' Romanian\"'
here's the code:
Code:
$file_contents_line = @file($csv_file['path'])
or die ("CSV file not found.");
foreach ($file_contents_line as $key => $val)
{
// Skip empty lines
if (empty($val)) continue;
$inserts .= (($key >= 1) ? ", " : "")."('";
$values = explode($csv_file['sepa'], $val);
for ($j = 0; $j < count($values); $j++)
{
$inserts .= addslashes(utf8_decode($values[$j])).(($j != (count($values) - 1)) ? "', '" : "");
}
$inserts .= "')";
}
i'm guessing my issue is somewhere on this line:
Code:
$inserts .= addslashes(utf8_decode($values[$j])).(($j != (count($values) - 1)) ? "', '" : "");
What would be nice is if it replaced those commas with semi-colons and got rid of the double quotes, like this:
Original value:
Code:
"Spanish, French, Romanian"
Code:
'Spanish; French; Romanian;'
or maybe included those commas and just got rid of the double quotes, i don't know, but this line is causing errors in my SQL statement.
Thanks.