Importing CSV into a mysql DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrhitokiri
    New Member
    • Apr 2008
    • 35

    Importing CSV into a mysql DB

    ^--- i guess the title says it all.. ^_^

    so, how do you do this?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    ^--- i guess the title says it all.. ^_^
    Not really.
    Via which medium?
    A one-off or regular task?
    I could go on

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by jrhitokiri
      ^--- i guess the title says it all.. ^_^

      so, how do you do this?
      You'll probably end up choosing one from these 3 approaches
      1.) Using the LOAD DATA INFILE statement.
      2.) By importing the file as a mysqldump file (If the csv file was created using mysqldump)
      3.) By using an import script written in a programming language that can access MyQSL and can read csv files

      Comment

      • jrhitokiri
        New Member
        • Apr 2008
        • 35

        #4
        oh, yeah, i forgot... I'm using PHP for this job. so how do you do this?

        although i have started, I have this form:

        Code:
        <INPUT type="file" name="filename" enctype="multipart/form-data" style="FONT-SIZE: 11px; FLOAT: right; FONT-FAMILY: Tahoma">
        and here's the PHP code

        Code:
        if (isset($_POST['file_submit']))
        {
           if (!empty($_POST['filename']))
           {
               $filename = addslashes($_POST['filename']);
               echo $filename;
               $handle = fopen($filename, "r");
               while (($data = fgetcsv($filename, 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);
            }
           else echo "<B><center><FONT face=Tahoma color=#ff0000 size=3>Filename field empty. Please fill up all the fields.</b></FONT></center><br>";
        }
        here's what the echo $filename outputs when i select a file

        a.csv

        and after that, some error messages.

        Warning: fopen(a.csv) [function.fopen]: failed to open stream: No such file or directory in C:\Documents and Settings\jcrequ iroso\Desktop\x ampplite\htdocs \inventory\add. php on line 50

        Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\Documents and Settings\jcrequ iroso\Desktop\x ampplite\htdocs \inventory\add. php on line 51

        Warning: fclose(): supplied argument is not a valid stream resource in C:\Documents and Settings\jcrequ iroso\Desktop\x ampplite\htdocs \inventory\add. php on line 52

        i think i have to copy the file to the root folder of the localhost, and then access it, is that right? if so, how can i do this?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          It's a PHP problem then so I'll move it to that forum.
          P.S The error messages seem to be giving you all the hints.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Now isn't your second post much more informative than your first?
            As r035198x said the clues are all there.[PHP]$handle = fopen($filename , "r");
            while (($data = fgetcsv($filena me, 1000, ",")) !== FALSE)[/PHP] You are trying to open a non-existent file.
            Then you are trying to read a non-existent file with the file name instead of the handle [PHP]if($handle = fopen($filename , "r"))
            while (($data = fgetcsv($handle , 1000, ",")) !== FALSE)
            else
            echo 'Failed opening file'.$filename ;[/PHP]

            Comment

            • Justummar
              New Member
              • Jun 2008
              • 5

              #7
              or you could use
              Code:
              $sql = "LOAD DATA LOCAL INFILE 'xfile.txt' INTO TABLE `lettings` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES;";
              mysql_query($sql) or die('Error loading data file.<br>' . mysql_error());

              Comment

              Working...