Jump to content

CSV into mySQL


petenaylor

Recommended Posts

Hi all

 

I am am trying to use the following piece of code to import a CSV file into a mySQL database:

 

$filename = $_FILES['sel_file']['tmp_name'];
         $handle = fopen($filename, "r");
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
         {
         mysql_query("INSERT INTO user SET id = '".$userid."', name = '".$data[0]."', email = '".$data[1]."', phone = '".$data[2]."'") or die(mysql_error());
         }
    fclose($handle);

 

It works great except it only imports one line in the CSV file. How do I get it to keep adding the lines into the DB?

 

My CSV looks like this:

 

Pete Naylor,[email protected],0800101101

Bob Jones,[email protected],08700123123

 

Many thanks for for help

 

Pete

Link to comment
https://forums.phpfreaks.com/topic/255631-csv-into-mysql/
Share on other sites

if you try with:

$filename = $_FILES['sel_file']['tmp_name'];

        $handle = fopen($filename, "r");

        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)

        {

print_r($data);

        mysql_query("INSERT INTO user SET id = '".$userid."', name = '".$data[0]."', email = '".$data[1]."', phone = '".$data[2]."'") or die(mysql_error());

        }

    fclose($handle);

 

what is the output?

Link to comment
https://forums.phpfreaks.com/topic/255631-csv-into-mysql/#findComment-1310471
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.