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,test@mail.com,0800101101

Bob Jones,bob@mail.com,08700123123

 

Many thanks for for help

 

Pete

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.