O'Reilly logo

CodeIgniter 2 Cookbook by Rob Foster

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating a CSV from a database result

Perhaps one of the most common things you'll be asked to do, especially if you are building a complex application that may have users, products, orders, and various other metrics is to provide some sort of reporting of that data. Perhaps you'll be asked to generate a CSV file, and the following sections show how you do it.

Getting ready

Firstly, we need to create a table and enter some example data so that you'll see some data in the CSV format, so with that in mind, copy the following code into SQL:

CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_first_name` varchar(125) NOT NULL, `user_last_name` varchar(125) NOT NULL, `user_email` varchar(255) NOT NULL, `user_created_date` ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required