tags:

views:

361

answers:

2

Hey guys, here is my problem. I am writing a Drupal module. A user uploads a CSV list of e-mail addresses to a server in a form. I need to do a JOIN on my database to get a list of unique addresses from the CSV file and the DB (No, it is not for spam :).

How would I turn the uploaded CSV file into a format that I can do an SQL join on it with a table in my db in php?

+2  A: 

You can load it directly using LOAD DATA INFILE

Or, if you prefer, read it in PHP with fgetcsv(), munge to your heart's content, and them INSERT into a temporary table.

Frank Farmer
One question: where do I need to put the file for MySQL to be able to access it?
Jergason
I believe mysql can access the file anywhere, if you give it an absolute path, and the mysql user has read permission on that file
Frank Farmer
+1  A: 

Loading the data into a "temporary" table makes it very easy to deal with. You will want to apply an index to the table to speed joins, but it may be faster to drop the index before loading the data, and then recreate it afterwards. (As with all performance advice, measure your own results before adopting a more complex solution.)

yukondude
How would I load it into a temporary table? Just create a new table for each file upload and drop it after I am done with the join?
Jergason
Sorry, more specifically, how would I create a temporary table from the file? I think I understand how to create the table, but how would I fill it in from the uploaded CSV file?
Jergason
If the CSV data is always in the same format, then you might as well create a regular table that you consider "temporary" and just TRUNCATE its contents after each load. Loading the data in bulk is RDBMS-specific, but you could always translate each line in the file to an INSERT statement in PHP.
yukondude
Great, thanks for your help.
Jergason