Export data in csv file php

Export data from database in mysql/codeigniter in a csv file.
In this example, the fputcsv() function accepts an array of database results and a file resource pointer. This array of results is converted into the CSV format and written to the target .csv file with the reference of the given file pointer.

Copy and paste the below code:
Change 1: Create file named data.csv and place it in the folder.
Change 2: Replace the table name in the variable $tableName.
Change 3: Add column name in the array format in the variable $colmns.

That’s it. Now file named data.csv is ready to download, which contains data fetched from database.