How to import CSV data into MySQL with php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apssiva
    New Member
    • Feb 2011
    • 26

    #1

    How to import CSV data into MySQL with php?

    Hi,


    I need to update every column base on nric and year.

    I use this code read and print the entire contents of a CSV file
    Code:
    $row = 1;
    if (($handle = fopen("student.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";
            }
        }
        fclose($handle);
    }
    The above coding is correct or wrong.

    i don't know, how to update every column base on nric and year in my database?

    query is "INSERT INTO student (meta_aps) SELECT nric, year FROM student WHERE nric= 'this data from csv file'";--> This is correct

    please give some idea.....
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    What kind of CSV is this?

    can you actually brows through the data?

    Comment

    • apssiva
      New Member
      • Feb 2011
      • 26

      #3
      the data is in excel file, so i saveas into CSV

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        I think you should learn CSV structure a little more....

        Comment

        • apssiva
          New Member
          • Feb 2011
          • 26

          #5
          Hi,

          Below is my output, i want to Import CSV data into MYSQL database. pls only one help me.
          how to update every column base on nric and year in my database?

          Comment

          Working...