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


33 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

Unknown 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
------------------
شركة نقل اثاث بالدمام

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

chenmeinv0 said...

guess
flip flops
ray ban outlet
ugg
polo ralph lauren
polo lacoste
replica watches
polo ralph lauren
sac burberry
hogan
2018.1.15xukaimin

Jason said...

This is awesome
http://mobihackers.com/

Umer Ali said...

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

Bilal Ameen said...

Good Artical,Thanx for shared.software development company 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 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...

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

Ahsan12 said...

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

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

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

etree said...

Now you can buy almost any plant online across cheer tree Pakistan in major cities including Lahore, Islamabad, Rawalpindi, Karachi and many more. Delivery Service Areas.

WebNagar said...

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

WebNagar said...

I really appreciate this wonderful post that you have provided for us.
Regards,
Email Marketing in Karachi

Beacon Builder said...

This ia such an amazing and informative post. I really appretiate you have shared all the important details about the topic. Beacon Builder in Pakistan

b3 shearling flight jacket said...

Great to see such a post on the web, thanks for sharing such with us.

Post a Comment

Cron Design Studio: Dublin based web design & software development