This is a discussion on "How to export a file from mysql to csv format?" within the PHP Forum section. This forum, and the thread "How to export a file from mysql to csv format? are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
How to export a file from mysql to csv format?
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
How to export a file from mysql to csv format?
I would like to export the data from php or mysql to excel format. If I click on a button, then export to csv for excel format and display it on the screen!
How do I do that? I found tutorials from excel to php .. not php to excel Thank you, |
|
|
|
|||
|
Re: How to export a file from mysql to csv format?
Are you using phpmyadmin?
|
|
||||
|
Re: How to export a file from mysql to csv format?
Here you go:
http://www.modwest.com/help/kb6-135.html A CSV file is a very simple text file in the following format:
In order to make the browser recognize your file as a .CSV file, you'll need to modify a couple of headers. The link above suggests the following:
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
||||
|
Re: How to export a file from mysql to csv format?
Here's a mysql to Excel method. I don't export to Excel a lot, but I do format delimited strings a lot. This is off the top of my head so there will be a bug/typo or two.
You may have to flip them around a bit to conform to your Excel format. I find it easiest to put them on an Excel page first and use Excel to reformat. YMMV. You should also consider tab delimited fields. I'm finding them easier to deal with than csv's and IIRC Excel actually prefers them. ('\t') Also, I think the mysql default is semicolon field delimiters, but that's a pain when PHP is involved. This comes in handy for a lot of different situations. It's such an easy tool to experiment with. You can implode/explode in two short lines to try a different delimiter. I use it for db updates, too. Say you have a db with a LOT of columns, most of which can be NULL. You want to handle a POST-method form that can contain data for any combination of one or more fields. (For non-null columns, you have to add some code. You also have to write exceptions for POST values that don't correspond to database fields.) Give the form inputs names which are the same as the column names. (Actually, you can use an abbreviation for the form and then replace it from a second 'key' db, but I'm trying to keep this simple, so let's assume there is an input with a name identical to each column name.) The form handler receives a nice $_POST array with the column names as the keys and the data as the value. Making these into two parallel arrays is fast and simple,eg:
There are several ways to handle non-null columns. It is easiest to deal with them if you group them all at the start of the table. Then require form input for all non-NULL fields (throw an error if the value isn't set or is empty); for non-unique fields, if you want to use a default, just make the default value of the form input the same as the default of the corresponding column (but you'll still want to require a value to be set). To retain more control, you can always set the values of non-NULL fields specifically. Then when you have massaged them into submission, set $i to the first NULL column. I wouldn't recommend it, but it certainly works, eg:
As you may or may not know, I am building a nutritional database that is up to @80 columns and @7500 rows. This method is invaluable in adding new rows and updating existing rows, where I may want to put values into anywhere from 1 to 79 columns (depending on what information is available for the food). Having NULL columns is an easy way to differentiate between nutrient content of 0 and nutrient content of unknown (important for nutrients people want to avoid, like saturated fat), while allowing me to use small and speedy numeric column-types. Last edited by masonbarge; May 20th, 2007 at 15:28. Reason: oopsie |
|
|||
|
Re: How to export a file from mysql to csv format?
Thanks guys for your replays !!! I tried your codes they are working great
|
|
||||
|
Re: How to export a file from mysql to csv format?
Another happy customer...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
![]() |
| Tags |
| csv, excel, mysql, php |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unexpected File Format | Lchad | Flash & Multimedia Forum | 8 | Aug 21st, 2007 18:48 |
| Convert date to MySQl format | AdRock | PHP Forum | 4 | Jan 15th, 2007 09:41 |
| gradient file format | aseriouslyfunkydiva | Graphics and 3D | 1 | Apr 6th, 2006 09:15 |
| asp file upload script having problems with MYSQL | paulmcn | Classic ASP | 2 | Oct 5th, 2005 18:45 |
| file upload asp-mysql database | fluff | Classic ASP | 7 | Aug 19th, 2004 10:45 |