title: Export data to csv using wpdb database
body: <p>I want to export data of a sql table in csv. I am using the follwoing code :</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['export']))
  {
            $file = 'email_csv'; // ?? not defined in original code
            $results = $wpdb-&gt;get_results("SELECT * FROM `scholarship_uesr`;",ARRAY_A);
  
            if (empty($results)) {
              return;
            }
  
            $csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;
  
            foreach ($results as $row) {
              $csv_output .= '"'.implode('";"',$row).'";'."\n";
            }
            $csv_output .= "\n";
            $dir = "/home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/csv/";
            $filename = $file."_".date("Y-m-d_H-i",time()).".csv";
  
            header("Content-type: application/vnd.ms-excel");
            header("Content-disposition: csv" . date("Y-m-d") . ".csv");
            header( "Content-disposition: filename=".$filename.".csv");
            print $csv_output;
            exit;
          //  echo "&lt;div class='alert alert-info'&gt;Data Written to CSV File. &lt;a href='".$geturl."' download&gt;Click Here to Download&lt;/a&gt;&lt;/div&gt;";
  }
  $allsubs = "SELECT * FROM `scholarship_uesr`";
  $select_query = $wpdb-&gt;get_results($allsubs);
  //echo $wpdb-&gt;last_error;
  ?&gt;
  &lt;form method="post" action="#" enctype="multipart/form-data"&gt;
  &lt;input type="submit" name="export" class='btn btn-danger' value="Export Data to CSV !!!"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>When I am using to this code then it is directly print the data of database instead of downloading the file.</p>
  
  <p>When i turn on the <code>error_reporting</code> then it is throwing error :</p>
  
  <pre><code>Warning: Cannot modify header information - headers already sent by (output started at /home/modifoun/public_html/wp-admin/includes/template.php:2041) in /home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/student_list.php on line 45
  
  Warning: Cannot modify header information - headers already sent by (output started at /home/modifoun/public_html/wp-admin/includes/template.php:2041) in /home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/student_list.php on line 46
  
  Warning: Cannot modify header information - headers already sent by (output started at /home/modifoun/public_html/wp-admin/includes/template.php:2041) in /home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/student_list.php on line 47
  </code></pre>
  
  <p>How can I export data to csv using wordpress</p>