Home > Software design >  How to Export MySQL Data using JavaScript?
How to Export MySQL Data using JavaScript?

Time:11-28

I have created a website where I display records based on the user search. But, I want to give the users an option to download the fetched records as a csv file. How can I export fetched MySQL data into a CSV using JavaScript?

CodePudding user response:

use datatables, here is an exemple https://datatables.net/extensions/buttons/examples/initialisation/export.html

CodePudding user response:

<?php
 header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=\"report".".csv\"");
        header("Pragma: no-cache");
        header("Expires: 0");

        $handle = fopen('php://output', 'w');
        fputcsv($handle, array("SNo","Complain_id","Name","Email","Number","Address"));
        $cnt=1;
        foreach ($complains as $key) {
            $cnt  ;
            $narray=array($cnt,$key->fld_complain_id,$key->fld_customer_name,$key->fld_customer_email,$key->fld_customer_number,$key->fld_customer_address,);
            fputcsv($handle, $narray);
        }
            fclose($handle);
        exit;
  • Related