954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

CSV file uploading into a MySQL Database?

Good Morning,

I have a CSV file with thousands of rows of data, and I want to have this data uploaded into a table in a MySQL database. I also want an area where the file could be selected from the computer for the data to be uploaded.

Could this be done?
If it could, kindly guide me through getting it done...

May

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 

Have you tried phpmyadmin ?

Daedal
Junior Poster in Training
75 posts since Apr 2008
Reputation Points: 23
Solved Threads: 12
 
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hi All,

To Daedal...no, I haven't tried phpmyadmin...I would look into that now...

To Pritaeas...I would also now look at the website you have given to me...

To All...I will keep you all posted on my progress...

Thank much in advance...
May

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 

Hi All,

Well I did look at the links you gave and the phpmyadmin isn't what I was looking for, and as for the website link, I actually looked at that site before, and it helps me understand a lil bit about the uploading but it does not allow the user to actually select a file (with a browse button...sorta like thos picture uploader, but this time it's a CSV uploader)...I hope I am not confusing you...

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 

How about letting MySQL do the dirty work:

load data local infile 'infile.csv' into table yourtable
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(column1,column2,column3)


This example is for a line that looks like this: 'data','data','data'
(delimited by a comma and enclosed by quotes

Be sure to remove any headers or other non-important data from the csv file

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Yea, but the files to be uploaded are monthly...and are therefore named differently...so I can't have a static name in the code...

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 

niek_e provided a good example. Use what he has provided, and then implement an upload script. Just substitute the 'infile.csv' for the file that has been uploaded. You're asking for a lot, eh? Everything done for you? Teach a man to fish... :)

Daedal
Junior Poster in Training
75 posts since Apr 2008
Reputation Points: 23
Solved Threads: 12
 

Hi,
Ok, I'll give it a try and keep you all posted...:)
Oh, and nope, I do not want that everything be done for me...I was merely stating what I want to get done, and if there's anyone who can guide me along....:)...if it came across that way, then I must say that that was not my intention...
so, like I said, I'll give niek_e's example a shot, and I'll let you all know...:)
Thanks much in advance...:)
May

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 

No no, you're fine... maybe start a new thread about handling PHP file uploads.

Daedal
Junior Poster in Training
75 posts since Apr 2008
Reputation Points: 23
Solved Threads: 12
 

So that piece of code works, I am able to upload the data into a table in the database....but that was done through MySQL Query Browser...I gotta get that piece of code to work with my upload button now...

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 

Ok, well I am closing this thread...I will start with a new thread...

maydhyam
Posting Pro
562 posts since Feb 2008
Reputation Points: 48
Solved Threads: 1
 
<?php
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$dbname = 'dbname';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
	mysql_select_db($dbname);
	
         $fname ="something.csv";
        $chk_ext = explode(".",$fname);
        if(strtolower($chk_ext[1]) == "csv")
         {
        
             $filename = "something.csv";
             $handle = fopen($filename, "r");
       
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
             {
				 //print_r($data);
                $sql = "INSERT into users(name,lastname,email) values('$data[0]','$data[1]','$data[2]')";
                
				
				mysql_query($sql) or die(mysql_error());
             }
       
             fclose($handle);
             echo "Successfully Imported";
         }
         else
         {
             echo "Invalid File";
         }   
   ?>
shailendra2614
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You