0

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);
}
?>
0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.