How to export data as CSV from database in CodeIgniter

In CodeIgniter we can export data easily from database as CSV using a library called dbutil. We can pass the query result directly into the dbutil function and we can download the data as CSV.

In your model, write down a function called exportCSV as mentioned below.

function ExportCSV()
        $delimiter = ",";
        $newline = "\r\n";
        $filename = "filename_you_wish.csv";
        $query = "SELECT * FROM table_name WHERE 1";
        $result = $this->db->query($query);
        $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
        force_download($filename, $data);

You can change the filename and the database query as per your needs. Call this function from your controller.

Follow me


Web Developer at Energy Alternatives India
I'm interested in developing Web applications andkeen on learning new technologies.

View Nagarajan's Profile
Follow me

Latest posts by Nagarajan (see all)

  1. naren
  2. naren

Leave a Reply

Your email address will not be published. Required fields are marked *

займ на карту онлайн круглосуточно