I need to parse hard csv file and put it to MySQL.
1.csv:
"col1,""col21, col22, col23"",""col3"",""col41, col42""
My test php script file:
<?php
$connection = mysql_connect('127.0.0.1:3307','a','') or die ("Unable to connect!");
mysql_select_db('test') or die ("Unable to select database!");
mysql_query('SET NAMES utf8');
$fp = fopen("1.csv", "r") or die("Couldn't open filename");
while (($data = fgetcsv($fp, 0, ",", "'")) !== FALSE)
{
$import="INSERT INTO iptable(col1,col2,col3,col4) values('$data[0]','$data[1]','$data[2]','$data[3]')";
mysql_query($import) or die(mysql_error());
}
fclose($fp);
echo '<b>Successfully added to DB</b>';
?>
I need to INSERT INTO iptable VALUES('col1', 'col21, col22, col23', 'col3', 'col41, col42'). How i can to do that? (how to correctly separate them?).
My csv file is not correctly, but i need to parse data from it. To iptable(col1,col2,col3,col4). (4 columns)
For example, Excel is correctly convert this csv file to columns. (http://goo.gl/xm0bly)
LOAD DATA LOCAL INFILEto load it into MySQL. - Barmarcol21, col22, col23smells of bad design from a mile, same goes forcol41, col 422. you need to generate a better CSV, because this one is beyond messy 3. if you can't do (2) you must define a better separator, because the comma is ambiguous as the double quotes. - STT LCU