Database CSV Class July 2, 2008 5 Comments
I was getting kind of frustrated with some of the lack of Database CSV scripts I was finding. None of them really did what I wanted – of course not every script out there is suited for your own personal needs. I thought I would whip something up that’s pretty flexible when thrown into various situations.
This class is meant to extract data from a database and put it into a .csv document. With this class you can set to be prompted to download the csv, write the csv to a folder on your server or both. With different options and settings to choose from, this is a recipe for one happy export.
Any suggestions/comments/improvements are welcome.
Usage:
Example 1
require('database_csv.class.php'); $c = new Database_CSV(); $c->sqlTable('articles'); $c->fileName('articles.csv'); $c->export(); // accepts 'both', 'server' and 'download' (defaults to download)
Example 2
require('database_csv.class.php'); $c = new Database_CSV(); $c->sqlTable('articles'); // the table name OR select sql statement $c->fileName('myfile.csv'); $c->humanizeFields(true); // optional (defaults to false) $c->export('both'); // accepts 'both', 'server' and 'download' (defaults to download)
Example 3
require('database_csv.class.php'); $c = new Database_CSV(); $c->sqlTable('articles'); // the table name OR select sql statement $c->fileName('myfile.csv'); $c->saveTo('backups/test/another/teasd'); // optional (where you want the .csv to be put on your server) $c->export('server'); // accepts 'both', 'server' and 'download' (defaults to download)
Example 4
require('database_csv.class.php'); $c = new Database_CSV(); $c->sqlTable('SELECT * FROM articles LEFT JOIN article_tags ON article.id = article_tags.art_id'); // the table name OR select sql statement $c->fileName('myfile.csv'); $c->ignoreFields(array('id')); // optional $c->renameFields(array('title' => 'The Title')); // optional $c->humanizeFields(true); // optional (defaults to false) $c->errors(true); // optional (defaults to false) $c->export(); // accepts 'both', 'server' and 'download' (defaults to download)
Descriptions
- sqlTable: (String) Accepts an SQL SELECT statement or a table name. This is how we get the results for the .csv file. [required]
- fileName: (String) Specify what you want the output .csv to be named. If this is not set, it will default to “export.csv”. [optional]
- ignoreFields: (Array) If you don’t want certain fields to show up the .csv document you can set them to be ignored. Example array(‘id’,'another_field’) [optional]
- renameFields: (Array) If you would like to not have the column’s show up as their field name in the database you can change it to show up as something else. Example array(‘recipe_name’ => ‘Super Special Recipe Names’) [optional]
- humanizeFields: (Bool) If you have this set to true, fields like “recipe_name” will show up as “Recipe Name” and “grocery_list” will show up as “Grocery List”. If you have specified fields to be renamed with the renameFields function they will not be over-ridden. Defaults to false. [optional]
- errors: (Bool) If set to true and you made a boo boo, it will abort the .csv file creation and tell you the exact errors. Defaults to false. [optional]
- saveTo: (String) If you have export(‘server’) or export(‘both’) set, then you can specify where you want the .csv file to go on the server. If the folder is not there, it will attempt to create it. By default it will put the .csv in the same directory the script is run in. [optional]
- export: (String) This function has 3 different options: ‘download’, ‘server’ and ‘both’. Setting it to ‘download’ will prompt you to download the .csv file. Setting it to ‘server’ will just write the .csv to a place on the server. Setting it to ‘both’ will do exactly that. Defaults to ‘download’. [required]

Yes, I did leave out the username and password. That is something you'll need to include above.