Reputation: 1498
I have a csv file below the first row is the header.
I want to read all the rows from CSV and insert each columns from csv as each row in database table.
In the table the order will be as shown below
-------
TABLE
-------
ID Value
---------------
1 25-10-2013
2 12:53:35
3 test1
4 india
5 asia
6 26-10-2013
7 2:53:40
8 test2
9 uk
10 europe
11 27-10-2013
12 23:16:20
13 test3
14 dubai
15 asia
i tried using the belwo code
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
"insert statement here ""
}
}
fclose($handle);
}
?>
Upvotes: 0
Views: 131
Reputation: 1756
assuming that the ID column is auto increment, your query should be
INSERT table(value)
VALUES ('$data[$c]');
if isn´t autoincrement,
INSERT table(id,value)
VALUES ('$row','$data[$c]');
with mysql, is
$loadsql = "INSERT table(value)
VALUES ('$data[$c]');";
mysql_query($loadsql) or die(mysql_error());
i hope this can help you.
Upvotes: 1