In codeigniter, there is a database utilities class to export MySQL database data as CSV File. In order to initialize the utility class, ensure that the database class is running in your codeigniter application. F
Friends, follow these steps to export query results as CSV file using your codeigniter application.
Step 1: Download your codeigniter application and route your page.
step 2: Set database username,password and db name in
Step 3: Create a table and insert data to export data s csv file from your MySQL database.
Step 4 : Create a controller under
application/controllers/. Name it as Export.php.
Step 5 : Please copy below code to Export.php file and save it.
$this->load->dbutil(); - loads database utility class.
csv_from_result($query,$delimiter,$newline) - converst the Query results to csv file.
First parameter specifies the query to generate the result. Second parameter specifies
delimiter to separate the resultant data. Thrid parameter allows you to set the newline.
This database utility function generates a csv layout. So to generate and download a CSV file use force_download() function.
force_download($file) – Download the file.
Step 6: Create the view page in
application/views directory to export the data from mysql database to csv file on a button click. Name the file as export_v.php
Step 7: Copy and paste the code to your view file and save it.
Step 8: Open browser and type url to view the site.