Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,679 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,202 people online right now. Registration is fast and FREE... Join Now!




Importing Excel data into MySQL

 
Reply to this topicStart new topic

> Importing Excel data into MySQL, Hooray for delimiting!

Rating  5
akozlik
Group Icon



post 11 Jun, 2008 - 01:57 PM
Post #1


A post came up on the board that discussed importing an Excel file into a database. This is a very common task in application development, so I figured I'd show a quick and easy way to do this.

I am going to use an example of a school who wishes to import it's student id numbers and the first, middle, and last names of the student into the database. This hits close home to me because I just did it today. Ha ha.

Lets assume you have a table named students that has the fields 'id', 'firstname', 'middleinitial', 'lastname'. You also have an Excel file with the data you want inputted into the database. My Excel file contains four columns, each with it's corresponding data. I have about 350 fields.

The first thing you want to do is save your Excel in tab delimited format. This is super easy. Just select File > Save As . . . > Text (tab-delimited). I'm using Excel 2003. I imagine the same would apply for the new Office Suite Open Office. Go ahead and save your file as students.txt.

If you view your file in your favorite text editor it should look like the following

CODE

12345     Bob     B     Johnson
65458     Joe     L     Schmoe
98741     Jane     D     Davidson
32179     Sue     R     Lastname
68423     Ramone     Q     Smith


It'll probably look neater, but it will be separated by tabs, with a record on each line. Remember that each tab is actually read as a '\t' character.

Next we are going to write the PHP that will process the file. It looks like the following:

php

<?php

// Connect to your database

$fp = fopen("students.txt", "r") // Open the file for reading

while ($line = fgets($fp)) { // Loop through each line
list ($id, $first, $middle, $last) = split("\t", $line, 4); // Split the line by the tab delimiter and store it in our list
$sql = "insert into students (id, firstname, middlename, lastname) values ('$id', '$first', '$middle', '$last')"; // Generate our sql string
mysql_query($sql) or die ( mysql_error() ); // Execute the sql
}

?>


That should just about do it. Just realize that this is meant to be a basic example. If you have 300 rows in your file, you're going to be executing the sql 300 times. If optimization is important to you you'll need to optimize the code.

Pretty short and simple tutorial. I hope it's of some help to anybody out there trying to import Excel data. There are many applications to being able to do this, so I think it's a very important tool in your PHP belt. Leave your comments, suggestions, and criticisms at the beep. * beep *
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

chrisman
Group Icon



post 14 Jun, 2008 - 07:03 PM
Post #2
Thanks! I'm new to PHP, especially MySQL, and I knew you could do this, I just wasn't sure how.

PS: No "Thanks" link on this page. wink2.gif
Go to the top of the page
+Quote Post

akozlik
Group Icon



post 14 Jun, 2008 - 08:45 PM
Post #3
QUOTE

PS: No "Thanks" link on this page. wink2.gif


Ha ha, no problem. I'm glad you enjoyed the tutorial. Let us know on the PHP board if you have any more issues and we'll try to clear them up. I don't mind writing more tutorials if they're needed.
Go to the top of the page
+Quote Post

MrsQ
*



post 20 Sep, 2008 - 07:07 AM
Post #4
Thank you for a simple and straightforward explanation! I've been trying to figure out how to do that for quite some time now. I knew it was possible, but being a bit of a newbie to php and web design in general, I wasn't sure how. Although I tried to do searches for instruction, the answers were all a bit over my head. I look forward to learning more from DIC!
Go to the top of the page
+Quote Post

akozlik
Group Icon



post 20 Sep, 2008 - 07:20 AM
Post #5
QUOTE(MrsQ @ 20 Sep, 2008 - 11:07 AM) *

Thank you for a simple and straightforward explanation! I've been trying to figure out how to do that for quite some time now. I knew it was possible, but being a bit of a newbie to php and web design in general, I wasn't sure how. Although I tried to do searches for instruction, the answers were all a bit over my head. I look forward to learning more from DIC!


Awesome, I'm glad it really helped you out. Definitely come visit the forums if you have any specific questions you can't find answers to. Everybody's really helpful in there.

Out of curiosity, how did you end up finding this tutorial? I've written several for DIC, but a few of them have had a lot more exposure than the rest. This is one of those.

Glad you like it and welcome to the PHP world!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 06:40AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month