All the girlfriends that I've ever had have worn glasses. -Kevin Malone
Submit your own quote
  • home
  • about
  • lab
  • work
  • contact

CSV 2 PHP 2 MySQL Insert

I wrote something last night that will save a lot of people a lot of time. It's a class that opens a .csv file (normally saved through Excel) and parses your data accordingly to build an INSERT query for it. Then it inserts all of the information into whatever table you specified. You can also choose to create the table if it doesn't exist. Below are the parameters included in the class.

 

Download It!

 

PLAIN TEXT
PHP:
$csv = new CSVData();
$csv->csvFile('file.csv'); // The path to our file (file.csv)
$csv->mysqlTable('tablename'); // The table we are dealing with
$csv->makeTable(false); // Do we want to make the table if its not created, you must declare the proper params in the .csv (true/false)
$csv->testMode(false); // If we want to test or just go in head first (true/false)
$csv->response(); // this returns true if the executed query worked.  You can use an if statement to check this, followed by the error/success msg in the next param..
$csv->responseText(); // the error/success msg

 

In a few days I will have a video tutorial online explaining how to get things setup.

 

If you're a picture person, below is an illustration of what it would be doing.

 

excel2db

 

Posted February 20, 2008

4 Sexy Responses to CSV 2 PHP 2 MySQL Insert

chuck
had this to say...

for some reason it bombs if i change the file name. and if i change the data in the csv file it still spits out the same information over and over again rob, tim,… kim..
no matter what data is in there

March 25, 2008 at 2:18 am
Jordan Boesch
had this to say...

@chuck: It might be line 39 in test.php

$csv->csvFile($fileName);

Change that to read

$csv->csvFile($uploadFolder.$fileName);

March 25, 2008 at 6:19 am
Olaf Skaug
had this to say...

Let me just say that this is an awesome app. Just what i needed! But there are a couple of things I was wondering about. Does the class strip zeros from numbers? One of my fields has numbers like ‘0005′, but when i import, they turn into ‘5′. How can I remove this? And another thing. How could i do a DELETE FROM sometable WHERE someid = ‘0005′ BEFORE the INSERTion of the rows in the end? I tried doing it in the top of test.php, but then the added rows get deleted as well.

Thanks!

May 26, 2008 at 8:09 am
Jordan Boesch
had this to say...

@Olaf: You cannot delete using this class. I’m not sure why it’s making your number into 5. Try changing the data type to VARCHAR.

May 27, 2008 at 6:47 pm

 

Feel free to put in your 2 cents. I need cash.

Code must be put in between <code> </code> tags or there will be no soup for you.

 

Recent lab work

  • Database CSV Class
  • imageTick for jQuery
  • wiggleSlide for jQuery
  • CSV 2 PHP 2 MySQL Insert
  • jTabber for jQuery
  • Ajax Star Rating
  • Tiny Form Class

Things I like

  • cakephp
    CakePHP Rapidly develop robust web applications
  • jquery
    jQuery Traversing the DOM just got a lot simpler
  • asparagus
    Asparagus Even more nutritious than programming
© boedesign.com 2008 - God bless Wordpress