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


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:

Unknown said...

canada goose
coach factory outlet
coach outlet
ray ban sunglasses
fit flops
canada goose jackets
kate spade outlet
cheap oakleys
birkenstock sandals

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

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

Unknown 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

chenlili said...

moncler outlet
moncler pas cher
jordan retro
prada handbags
coach outlet online
ugg boots
ferragamo shoes
true religion jeans
ugg italia
converse shoes

chenmeinv0 said...

flip flops
ray ban outlet
polo ralph lauren
polo lacoste
replica watches
polo ralph lauren
sac burberry

Jason said...

This is awesome

Umer Ali said...

This is nice and informative for the web development services, and really love to read it.

Amany Said said...

هل تبحث عن شركة متخصصة فى خدمات التنظيف بالطائف بافضل المعدات والسوائل وثقة تمة فى العمل ودقة فى النتائج كل هذه المميزت توفرها شركة الخليج الشركة الافضل والامثل فى الخدمات المنزلية بالطائف وبما اننا الشركة الافضل والامثل بدون منافس سوف نسعى لتوفر افضل الخدمات باقل تكلفة وبقدر كبير من الاهتمام والدقة عزيزى اينما كنت فى اى منطقة ا وحى تابع لمدينة الطائف اتصل بنا وسوف نصلك فى الحال شركة الخليج للخدمات المنزلية شركة تنظيف منازل بالطائف
شركة تنظيف فلل بالطائف
شركة تنظيف خزانات بالطائف
شركة تسليك مجارى بالطائف
شركة رش مبيدات بالطائف
شركة مكافحة نمل ابيض بالطائف
شركة مكافحة حشرات بالطائف
شركة عزل اسطح بالطائف
شركة عزل خزانات بالطائف

Amany Said said...

تعمل شركة اللمسه في مكافحة الحشرات مثل النمل والصراصير وجميع انواع الحشرات بأبها الان ، وتعتبر شركة اللمسه من اشهر شركات مكافحة الحشرات في السعودية , لدينا عمالة مدربة ومتخصصة المواد المستخدمة فى مكافحة الحشرات غير ضارة على الانسان وليس لها روائح نعمل بدون مغادرة المنزل نعمل على راحة عملائنا .
شركة مكافحة النمل الابيض بنجران
شركة رش مبيدات بالخرج
شركة مكافحة حشرات بالخرج
شركة مكافحة النمل الابيض بالخرج
شركة رش مبيدات بأبها
شركة مكافحة حشرات بأبها
شركة مكافحة النمل الابيض بأبها
شركة رش مبيدات بنجران
شركة مكافحة حشرات بنجران
شركة رش مبيدات بالعينه

شركة ويب said...

شركة تنظيف بالباحة
شركة تنظيف منازل بالباحة
شركة مكافحة حرات بالباحة

شركة تنظيف بالطائف
شركة تنظيف خزانات بالطائف
شركة نقل اثاث بالطائف
شركة مكافحة حشرات بالطائف

Jessica Wil said...

Now you can win free stuff online, free giveaways, enter every day to win latest iphones, smartphones, macbooks, laptops, accessories, technology and much more at iFreeGiveaways - Win Competitions, Freebies & Giveaways Online!

Bilal Ameen said...

Good Artical,Thanx for shared.software development company in pakistan

Olivia Lua said...

outsourcingall.com "Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it.
This paragraph gives clear idea for the new viewers of blogging, Thanks you. You’re doing a great job Man, Keep it up.
Seo training
outsourcing training in dhaka
Best Website Development and Design Company in Bangladesh
free outsourcing training
graphic design training
digital marketing training
affiliate marketing training
outsourcing training

Saad Ali said...

To create a successful website, in addition to high-quality content, good SEO strategies are equally vital. SEO works for your website as seasoning works for your food, and 247 Developers provide the finest SEO services to help you reach the high ranks on a search engine. Working on Google guidelines, we develop result-oriented SEO campaigns for our clients including complete on-page and off-page optimization. That is how 247 Developers provide the Best SEO Services in Pakistan.

Skin specialist in Lahore said...

At Nasim Laser Skin Clinic, we are focused and committed to meet the increasing needs of our respected customers and valuable patients. Pakistan and its aesthetic laser technology have developed immensely in the past few years.

You no longer have to accept your look patiently. If you are not happy with your facial features you have the

power to change them and look younger.

We specialize in hair transplantation, cosmetic surgery, gyne aesthetics, laser treatments Skin specialist in Lahore ,and other advanced aesthetic procedures.

Our team of specialists and staff at Lahore clinic has years of experience with numerous patients and their different needs. With the help of our treatments, we have transformed their lives with invasive and non-invasive procedures to choose from. At Nasim Laser Center, we certainly have something for everyone.

bissavedu said...

mbbs in abroad

bissavedu said...

mbbs in russia

bissavedu said...

mbbs in ukraine

Naseem Laser Center said...

ehtelam treatment in lahore
dhant ka ilaaj in lahore
ehtelam ka ilaj in lahore
mushtzani ka ilaaj lahore
namardagi ka ellaj treatment
namardi ka ilaj in lahore
surat e anzal in lahore
shock wave therapy in lahore
oligospermia in lahore
azoospermia in lahore
surate e anzal in lahore
vaginismus treatment in lahore

Technosys Computers said...

Technosys Computers is a Canada based company with over nine years of experience in sales and repairing services of computers, laptops, mobile, and their accessories maintaining a good reputation over these years with our highly professional Computer Repair services in Brampton, Cell Phone repair services in Brampton
We are dedicated to pooling our knowledge, experience, tools, and collaborations to provide companies and consumers with a full one-stop solution with all their IT requirements, delivering value that meets their standards and providing them with a sustainable competitive edge

Web Development said...

A Pakistan based web development company. Our passion to create Innovative Web Design, Web development, Branding & Digital Marketing services.

Rahat Chohan said...

Thanks For Sharing This Valuable Information IT Consulting

unknown said...

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.

seo services in lahore

unknown said...

When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style.

seo company in lahore

unknown said...

If you are looking for more information about flat rate locksmith Las Vegas check that right away.

digital marketing agency in lahore

unknown said...

This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform!

web hosting in lahore

BMSAS Technologies said...

The post is written in very a good manner and it contains many useful information for me. Its full of information I am looking for and I love to post a comment. I am developer in BMSAS Technology. BMSAS Technology is the Software Development Company

Danny James said...

This Best Prayer App helps in learning Salat Namaz Prayers.

Ahsan12 said...

This post is really informative and helpful for qweb design and development services. Thanks for sharing.


With pandemic, the importance of short courses in rawalpindi has been increased rapidly as most of the people are losing jobs, which has gradually increased the rate of unemployment in Pakistan. This is why it is a need of time for the youth and professionals to be skillful, so they can start earning along with job or work. This will give them financial stability in a hard time.

yanmaneee said...

kyrie irving shoes
golden goose
yeezy sneakers
golden goose sneakers
curry 8
off white shoes
curry 6
jordan shoes
kyrie 5 spongebob
pandora jewelry

PNY Training23 said...

Ali quick is an extension for aliexpress to Find seller's ratings, similar products, price tracking, save images video. It is Best AliExpress product research tools to find aliexpress extension,
for DropShipping

Carol R. Nelson said...

Thanks For Sharing This Valuable Informationcustom wordpress development company

jack said...

I think this is among the most important info for me. And i am glad
reading your article. But want to remark on some general
things, The site style is great, the articles is really
nice. buy fake id online

plano towing said...

Thanks For Sharing This Valuable Informationcustom wordpress development company

OMT Lahore said...

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
SEO Services in Lahore

Digital Marketing Agency Lahore

OMT Lahore said...

You have performed a great job on this article.It’s very precise and highly qualitative. You have even managed to make it readable and easy to read. You have some real writing talent. Thank you so much.
Web Designing in Lahore

Post a Comment

Cron Design Studio: Dublin based web design & software development