18 September 2009

PHP class to Import/Export mySQL to CSV or Excel

Import/Export mySQL to CSV class:
This class (not really a class but thought the keyword "class" might help people searching for stuff like this) contains a function which can be fed a mySQL query and a filename. The query is run and the results are saved to a CSV file. There is also a function to import the CSV file back into a database.



I've used this in the past for site administrators who need to edit large amounts of table data. As long as you provide good, easy to follow instructions and maybe preface the import function with an error checker of some sort, this is a really handy way to forego the hassle of creating a html based data editor. People generally prefer to edit large amounts of data in Excel anyway so everyone's a winner!

Here's a quick preview of how it works:

include("scripts/mysql2csv.php");  //include the script
include("scripts/connect.php"); //connect to your database

$col_names_query="SHOW COLUMNS FROM products;";
$values_query="SELECT * FROM products;";
$fieldseparator=','; //excel needs comma rather than semi colon
$download_file=1; //should the file be downloaded from browser(1) or saved to the server(0)?
$filename = $table; //contains name of download file or (depending on $download_file) the path to save csv file without file extension.
exportCSV($values_query, $col_names_query, $col_count=NULL, $fieldseparator=',', $filename, $download_file);

Instructions and demo of how to use functions are enclosed:
Please see right hand pane before using my code - thanks!>>>
Download Now


9 comments:

Mike - CSV File Format Guru said...

Very useful post. I knew how to produce CSV file in Open Office, but getting the data into MySQL was a challenge. Thanks.

Muhammad Kashif Raza said...

Thankx. Very Usefull script. it would be helpful for me all the times..

Submit link Directory said...

thanks for post its save my lots of time

Anonymous said...

Can i use XLX instead of CSV for import and export . Can u please help me in this.

Web Design in Lahore said...

this is the fantastic one post. because of your post i feel like a good that i got something useful knowledge.

Anonymous said...

Great tutorial, here another one, it may help you too:
http://www.bewebdeveloper.com/tutorial-about-import-csv-to-mysql-and-export-from-mysql-to-csv-in-php

Meiqing Xu said...

canada goose
coach factory outlet
coach outlet
ray ban sunglasses
fit flops
canada goose jackets
kate spade outlet
cheap oakleys
birkenstock sandals
birkenstocks
20161228caiyan]
\

محمد محود said...

site administrators
------------------
شركة نقل اثاث بالدمام

林平平 said...

nike outlet store
mulberry outlet
ugg boots
north face jackets
nmd adidas
adidas nmd
ralph lauren uk
coach factory outlet
uggs clearance
louis vuitton outlet store

Post a Comment

Cron Design Studio: Dublin based web design & software development